C++ 中的非阻塞线程安全队列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1645326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
non-blocking thread-safe queue in C++?
提问by jldupont
Is there a thread-safe, non-blocking queue class in the C++?
C++ 中是否有线程安全的非阻塞队列类?
Probably a basic question but I haven't been doing C++ for a long time...
可能是一个基本问题,但我已经很长时间没有使用 C++ 了...
EDIT:removed STL requirement.
编辑:删除了 STL 要求。
采纳答案by asveikau
Assuming your CPU has a double-pointer-wide compare-and-swap (compxchg8b on 486 or higher, compxchg16b on most amd64 machines [not present on some early models by Intel])... There is an algorithm here.
假设你的CPU有双指针宽比较并交换(486或更高compxchg8b,compxchg16b上最AMD64上[不存在于一些早期型号的英特尔])...有一种算法在这里。
Update: It's not hard to translate this to C++ if you aren't afraid of doing a bit of work. :P
更新:如果您不害怕做一些工作,将其转换为 C++ 并不难。:P
This algorithm assumes a "pointer with tag" structure which looks like this:
该算法假设一个“带标签的指针”结构,如下所示:
// Be aware that copying this structure has to be done atomically...
template <class T>
struct pointer
{
T *ptr;
uintptr_t tag;
};
Then you want to wrap the instructions lock cmpxchg{8|16}b
with some inline asm...
然后你想lock cmpxchg{8|16}b
用一些内联汇编来包装指令......
Maybe then you can write the queue node like this:
也许你可以这样写队列节点:
template <class T>
struct queue_node
{
T value;
pointer<queue_node<T> > next;
};
The rest is more or less a transcription of the algorithm I linked to...
其余的或多或少是我链接到的算法的转录......
回答by Clifford
This seems to have been a popular subject on Dr. Dobb's last year:
这似乎是去年 Dobb 博士的热门话题:
回答by sbi
Since the current C++ standard doesn't even acknowledge the existence of threads, there is most certainly nothing thread-safe in the STL or any other part of the standard library.
由于当前的 C++ 标准甚至不承认线程的存在,因此在 STL 或标准库的任何其他部分中肯定没有线程安全。
回答by KeatsPeeks
You need to implement it yourself or use a library implementing it. To do it yourself, you may want to have a look at this:
您需要自己实现它或使用实现它的库。要自己做,你可能想看看这个:
回答by lsalamon
STL containers are not thread-safe, you should implement your treatment for concurrent access.
There is this project (C++) that aims to serve concurrent accesses : CPH STL
and paper about.
STL 容器不是线程安全的,您应该实现对并发访问的处理。
有一个旨在为并发访问提供服务的项目(C++):CPH STL
和关于.
回答by Nikolai Fetissov
Short answer - no. STL does not concern itself with concurrency (at least on the specification level.) Current C++ standard says nothing about threads.
You can easily build such a queue on top of STL and Boost though - just wrap std::queue
and boost::mutex
in your custom class.
简短的回答 - 不。STL 不关心并发性(至少在规范级别上)。当前的 C++ 标准没有提及线程。
您可以轻松地建立在STL和升压虽然上面这样的队列-只是包装std::queue
和boost::mutex
您的自定义类。
回答by enthusiasticgeek
May be too late now. For future reference, this one is a good implementation of lock-free queues (built in thread safety with some caveats).
现在可能为时已晚。为了将来参考,这是无锁队列的一个很好的实现(内置线程安全,但有一些注意事项)。
Multi producer - Multi consumer
多生产者 - 多消费者
http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++
http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++
https://github.com/cameron314/concurrentqueue
https://github.com/cameron314/concurrentqueue
Single producer - Single consumer
单一生产者 - 单一消费者
http://moodycamel.com/blog/2013/a-fast-lock-free-queue-for-c++
http://moodycamel.com/blog/2013/a-fast-lock-free-queue-for-c++
回答by Matt Chambers
The currently unofficial Boost.Lockfreeis something to consider. I use both the FIFO and the atomic types.
当前非官方的Boost.Lockfree值得考虑。我同时使用 FIFO 和原子类型。