?
Solved

Need a Thread safe STL Queue in Both Win32 and Unix

Posted on 2004-10-08
3
Medium Priority
?
3,307 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 400 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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 viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

770 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