CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    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.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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.

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Thread-safe Queue class somewhere?


  5. #5
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    Re: Thread-safe Queue class somewhere?

    Thank you so much, Kirants!

  6. #6
    Join Date
    Jul 2011
    Posts
    10

    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
  •  





Click Here to Expand Forum to Full Width

Featured