是的std ::队列线程与生产者和多个消费者安全 [英] is std::queue thread safe with producer and multiple consumers

查看:174
本文介绍了是的std ::队列线程与生产者和多个消费者安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使一个队列线程安全的?我需要推/流行/正面/背面,清晰。有升压相似的地方?

how can I make a queue thread safe? I need to push / pop / front / back and clear. is there something similar in boost?

我有一个生产者和一个或多个消费者

I have one producer and one or more consumer.

推荐答案

您必须保护访问的std ::队列。如果您在使用升压保护它使用的boost ::互斥。现在,如果你有多个读者和一位作家线程看看的boost :: shared_lock (读者)和的boost :: unique_lock (对于作家)。

You must protect access to std::queue. If you are using boost protect it using boost::mutex. Now if you have multiple readers and one writer thread look at boost::shared_lock (for readers) and boost::unique_lock(for writer).

然而,如果你将遇到的作家线程匮乏看看的boost :: shared_mutex

However if you will encounter writer thread starvation look at boost::shared_mutex.

这篇关于是的std ::队列线程与生产者和多个消费者安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆