-
September 29th, 2006, 05:11 AM
#1
Thread-safe Queue class somewhere?
I need a thread-safe queue (FIFO).
One of the methods have to be a blocking "Dequeue" (like "recv" in SOCKET).
I'm about to start this implementation, but I think it's something so common that it has to be already done...
I have googled for "thread safe queue c++ class", and I found a lot of interesting articles, but no link to actual code. Nothing in "Code Project" either.
Just not to feel silly about reinventing the wheel, I wanted to ask you codegurus: Is there really no code for this for me to use, somewhere? I see that .NET has already a nice Queue class, isn't there anything similar and thread-safe for VC++ 6.0?
Thank you!
Ricardo Vázquez.
-
September 29th, 2006, 10:20 AM
#2
Re: Thread-safe Queue class somewhere?
I doubt there is anything that is delivered with Dev Studio. The STL itself is not threadsafe, due to the overhead involved (STL was written to be fast).
You should be able to easily wrap an STL component to make it threadsafe.
Viggy
-
October 2nd, 2006, 12:40 PM
#3
Re: Thread-safe Queue class somewhere?
I ran across this same dilemma a few years ago and decided to just write my own. I did this using POSIX threads. My main container was a deque<T>. There was a pop() method that had the option of returning immediately, or would wait until something showed up in the queue. There was also a timed pop() method that would timeout after a specifed period of time. Other concepts that became necessary to implement were things like a thread-safe-counter and a mutex class that would lock upon instantiation and unlock when destroyed. Anyway, it was fairly easy to implement. You just have to pay attention to some of the nuances that may bite you, like cleanup handlers. Hope some of these ideas/thoughts help you. Good luck.
-
October 4th, 2006, 10:43 PM
#4
Re: Thread-safe Queue class somewhere?
-
October 5th, 2006, 02:17 AM
#5
Re: Thread-safe Queue class somewhere?
Thank you so much, Kirants!
-
July 1st, 2011, 04:49 PM
#6
Re: Thread-safe Queue class somewhere?
I have a related problem (Win32).
A queue thread processes a STL queue. When the queue is empty it waits infinitely for hResumeQueueThread (an auto-reset event) and then sets p = queue->First(), processes the data and finally, EnterCriticalSection() ... queue->pop() ... LeaveCriticalSection() (and loops).
In another thread a CompletionRoutine (for ReadDirectoryChangesW()) does
EnterCriticalSection()
queue->push()
LeaveCriticalSection()
SetEvent(hResumeQueueThread)
I find that after a push() and SetEvent(), the queue thread may crash upon accessing queue->First().
In further testing, using "while" (my fix for now) instead of "if" ...
while ( queue->Empty() )
{
WaitForSingleObjectEx(...);
}
... I find that the WaitFor... sometimes happens twice; i.e., queue->Empty() is TRUE even after a push by another thread.
Can someone please explain what's happening and maybe suggest other fixes?
Thanks!
- Vince
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|