为什么解决producer-consumer的bounded buffer实例需要2个semaphores和1个mutex?

Why are two semaphores and one mutex required in solving bounded buffer instance of producer-consumer?

为什么在生产者消费者问题中使用有界缓冲区时除了信号量之外还必须使用互斥锁?

empty:semaphore(n)
full: semaphore(0)
mutex: semaphore(1)

"mutex"用于锁定Buffer。

"full"用于buffer为空时阻塞consumer

"empty" 用于在缓冲区满时阻塞生产者。

这就是为什么你需要 3 个信号量。

您可以轻松 google 代码,因此我不会将其粘贴到此处。