Solved

Need a Thread safe STL Queue in Both Win32 and Unix

Posted on 2004-10-08
3
3,286 Views
Last Modified: 2013-12-14
How to make a STL queue thread safe across platform (both Win32 and Unix)?

I have multiple threads which may add and remove an item from a queue. Also I need one solution working in both Windows 2000/XP and Unix.

Thanks
0
Comment
Question by:wenzhi
3 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 100 total points
ID: 12263353
Somewhat like that below should do the job.

Regards, Alex

#ifdef WIN32
#include <windows.h>
typedef CRITICAL_SECTION TMutex:
#else
#include <pthread.h>
typedef pthread_mutex_t TMutex;
#endif

class ThreadMutex
{
     TMutex m_mutex;
public:
     ThreadMutex()
     {  
#ifdef WIN32
         InitCriticalSection(&m_Mutex);
#else
         pthread_mutex_init(&m_mutex, pthread_mutexattr_default);
#endif
     }
     void enter()
     {  
#ifdef WIN32
          EnterCriticalSection(&m_mutex);
#else
           pthread_mutex_lock(&m_mutex);
#endif
       }
       void leave()
       {  
#ifdef WIN32
            LeaveCriticalSection(&m_mutex);
#else
            pthread_mutex_unlock(&m_mutex);
#endif
        }
};

template <class T>
ThreadSafeQueue : public queue<T>
{
private:
    static ThreadMutex m_mutex;
public:
    void push(const T& x) { m_mutex.enter(); std::queue::push(x); m_mutex.leave(); }
    T pop() { m_mutex.enter(); T t = std::queue::top(x); std::queue::pop(x); m_mutex.leave(); return t; }
};

0
 

Author Comment

by:wenzhi
ID: 12266218
Thanks, Alex. It's a great answer. But one question before I can accept the answer: Can std::queue be portable to Unix? i.e., Does Unix recogize the directive #include <queue> ?

Thanks,wenzhi
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 25 total points
ID: 12267346
You'll find queue in the C++ standard. Provided your compiler is relatively standards-compliant, you should be in business.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question