consumer/producer in c++

前端 未结 4 1698
灰色年华
灰色年华 2020-12-15 14:18

This is a classic c/p problem where some threads produce data while other read the data. Both the producer and consumers are sharing a const sized buffer. If the buffer is e

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 14:46

    Protect the queue accesses with a mutex, that should be it. A 'Computer Science 101' bounded producer-consumer queue needs two semaphores, (to manage the free/empty count and for producers/consumers to wait on, as you are already doing), and one mutex/futex/criticalSection to protect the queue.

    I don't see how replacing the semaphores and mutex with condvars is any great help. What's the point? How do you implement a bounded producer-consumer queue with condvars that works on all platforms with multiple producers/consumers?

提交回复
热议问题