Solved

Need a Thread safe STL Queue in Both Win32 and Unix

Posted on 2004-10-08
3
3,232 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
Comment Utility
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
Comment Utility
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
Comment Utility
You'll find queue in the C++ standard. Provided your compiler is relatively standards-compliant, you should be in business.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now