生产者消费者我应该使用静音或信号量
我有2个线程, producer
线程通过Internet插座接收一些数据,构造对象并将其添加到队列中, consumer
thread thread thread从同一队列和进行一些处理。
由于这两个线程分别通过重点和脱水来修改队列,因此我认为在两个线程中使用互斥X应该足够。这是一种很好的方法,还是我应该使用信号量?如果是这样,为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以做任何一个工作。这取决于实际设计的细节。您将同时允许多少个生产者/消费者请求?它真的限于两个线程,还是代码会随着更多请求而产生其他线程?
我发现此简短 blog 有趣。它讨论并比较了
mutex
和信号量
,并可能给您一些想法。Either can be made to work. It depends on details of the actual design. How many producer/consumer requests will you allow simultaneously? Is it truly limited to two threads, or will the code spawn others as more requests occur?
I found this short blog interesting. It discusses and compares both the
Mutex
andSemaphore
and may give you some ideas.Examples in C
One queue accessed by multiple threads using mutex
Blocking Queue in C++ using semaphores (Not C specific, but concept will be same.)