是使用std :: deque还是std :: priority_queue线程安全? [英] Is using std::deque or std::priority_queue thread-safe?
问题描述
可能的重复项:
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的有效STL项目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容器,你可以希望一个库实现允许多个读者
on一个容器和多个作者在单独的容器上。你不能希望库可以消除手动并发控制的需要,你不能依赖任何线程支持。
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线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!