is std::queue thread safe with producer and multiple consumers(对于生产者和多个消费者来说,std::queue 线程是安全的)
问题描述
如何使队列线程安全?我需要推/弹出/前/后并清除.boost中是否有类似的东西?
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::queue 的访问.如果你使用 boost 保护它使用 boost::mutex.现在,如果您有多个阅读器和一个编写器线程,请查看 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::queue 线程是安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对于生产者和多个消费者来说,std::queue 线程是安
基础教程推荐
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
