Solved

Need a Thread safe STL Queue in Both Win32 and Unix

Posted on 2004-10-08
3
3,302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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