Solved

Need a Thread safe STL Queue in Both Win32 and Unix

Posted on 2004-10-08
3
3,259 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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