views:

1149

answers:

6

Is there a thread-safe, non-blocking queue class in the C++?

Probably a basic question but I haven't been doing C++ for a long time...

EDIT: removed STL requirement.

+3  A: 

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.

sbi
OK then: how about outside of STL?
jldupont
Nothing that the standard guarantees will be thread-safe anyway. Real implementations can and do provide their own guarantees though.
Jerry Coffin
+1  A: 

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.

lsalamon
+2  A: 

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.

Nikolai N Fetissov
is the boost normally packaged in the popular distributions e.g. Ubuntu, Fedora?
jldupont
I think so, at least the vanilla RHEL installs at my job always have older Boost version under /usr/include that gets in way.
Nikolai N Fetissov
Keep in mind that the original question was about a *non-blocking* queue. A mutex is a way of blocking, so it's unlikely to qualify.
Jerry Coffin
You can always try_lock and bail - no blocking here :)
Nikolai N Fetissov
+7  A: 

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.

Update: It's not hard to translate this to C++ if you aren't afraid of doing a bit of work. :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...

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...

asveikau
-1: I am looking for a C++ class: I've got my own tried and tested version in C. I know the algorithm (but thanks anyways).
jldupont
@jldupont: -1 is too harash for that. Asveikau gave you a good link to an algorithm, and it takes just a few minutes to translate it to C++. -1 is totally uncalled for here
Rom
...that's why I haven't down-voted him... just gave him a mark. If you look at my profile, you'll see I am very generous... you won't see lots of down-votes handed by me.
jldupont
@asveikau: nice update... thanks!
jldupont
The hazard pointer based queue is another option - it allows free'ing unused memory back to the OS, instead of having to maintain it forever in a free list. http://www.research.ibm.com/people/m/michael/ieeetpds-2004.pdf Also see Relacy for sanity checking your lock free algorithm: http://groups.google.com/group/relacy
Greg Rogers
Why do you say -1 if you don't actually downvote? That's just confusing. :p
jalf
+2  A: 

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:

Implementing a Thread-Safe Queue using Condition Variables

Samuel_xL
Thanks for the link. It seems that would add another dependency to my project: boost library. No?
jldupont
That's right. Anyway, you'll eventually use Boost if you're a C++ developer. Nikolai N Fetissov's suggestion about boost::mutex is a good one.
Samuel_xL
Being a C++ developer does not always lead to Boost. I know everyone here seems to love it, but lots of projects get along with out it (and even ban it). Have a look at how Chromium implements this.
jeffamaphone
Policy issues, no more no less.
Samuel_xL
A condition variable is always used in conjunction with a mutex. That doesn't fit with the question's requirement for a non-blocking queue.
Jerry Coffin
+2  A: 

This seems to have been a popular subject on Dr. Dobb's last year:

Clifford