问题描述
可能的重复:
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 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)