Is using std::deque or std::priority_queue thread-safe?(使用 std::deque 或 std::priority_queue 线程安全吗?)
问题描述
可能的重复:
C++ STL std::set 线程安全吗?
STL 队列的线程安全
我猜不是,我只是想确定一下.意味着 2 个线程使用 相同 std::deque 同时使用 std::deque::push_back 或 push_front.
I'm guessing it isn't, I just want to make sure.
meaning 2 threads using the same std::deque using std::deque::push_back or push_front at the same time.
同样的问题适用于 std::priority_queue 和函数 std::priority_queue::push 和 std::priority_queue::pop..
Same question goes for std::priority_queue and the functions std::priority_queue::push and std::priority_queue::pop..
那些容器是线程安全的吗?或者我应该亲自将其编程为线程安全的?
Are those containers thread-safe? Or I should personally program it to be thread-safe?
Tnx 很多.
推荐答案
来自 Scott Myer 的 Effective STL Item 12.对 STL 容器的线程安全有现实的期望
From Scott Myer's Effective STL Item 12. Have realistic expectations about the thread safety of STL containers
多个阅读器是安全的.多个线程可以同时读取单个容器的内容,这将正常工作.自然,在读取过程中不能有任何写入者对容器进行操作.
Multiple readers are safe. Multiple threads may simultaneously read the contents of a single container, and this will work correctly. Naturally, there must not be any writers acting on the container during the reads.
不同容器的多个写入器是安全的.多个线程可以同时写入不同的容器.
Multiple writers to different containers are safe. Multiple threads may simultaneously write to different containers.
当涉及到线程安全和 STL 容器时,您可以希望有一个允许多个读者的库实现在一个容器上,多个编写器在不同的容器上.您不能指望该库消除手动并发控制的需要,也不能完全依赖任何线程支持.
When it comes to thread safely and STL containers, you can hope for a library implementation that allows multiple readers on one container and multiple writers on separate containers. You can't hope for the library to eliminate the need for manual concurrency control, and you can't rely on any thread support at all.
这篇关于使用 std::deque 或 std::priority_queue 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 std::deque 或 std::priority_queue 线程安全吗?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
