Skip to content
Herbert Koelman edited this page Mar 28, 2016 · 4 revisions

C++ POSIX Threads library

object

Some compilers are not fully implementing C++11 stendard, IBM's xlC 13.1.2 is laking the new concurrency features that the standard brings.

This wrapper intends to bring these feature by wrapping the pthread library as a replacement for the C++11 classes.

WARNING this is a replacement of some C++11 features, it is best to use the standard implementation if your compiler support it.

posix Threads

POSIX Threads, usually referred to as Pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API.

POSIX Threads is an API defined by the standard POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995).

Implementations of the API are available on many Unix-like POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD, Linux, Mac OS X and Solaris. DR-DOS and Microsoft Windows implementations also exist: within the SFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also within third-party packages such as pthreads-w32, which implements pthreads on top of existing Windows API.

library's content

To use the library include `#include "pthread/pthread.hpp" to use the actual set of wrappers:

  • pthread::mutex: handles a pthread mutex structure
  • pthread::lock_guard: this class is in charge of locking and unlocking a given mutex.
  • pthread::condition_variable: handles a pthread condition variable.
  • pthread::thread: wrap's into a class a pthread thread.
  • pthread::pthread_exception: base class off pthread exceptions

using pthread::mutex

This code scrap illustrates how to use mutexobjects:

pthread::mutex mtx;

// beginning of a critical section, get mutex
{
  pthread::lock_guard<pthread::mutex> lck(mtx); // lock mutex

  // do something critical
  ...
} // lock_guard is freed and the mutex is unlock

using pthread::condition_variable

To wait for special conditions to be met you use a condition variable. A condition variable blocks the current thread and waits to be signaled by another thread when the wanted condition was met. Condition variables are usefull to implement producer/consumer pettern.

This sample synchronized queue uses mutexes and condition_variable objects.

template<typename T> class sync_queue {
public:
  void put (const T& item, int wait_time);
  void get ( T& item, int wait_time );

  sync_queue( int ms = 10 );
  virtual ~sync_queue();
private:
  pthread::mutex              mtx;
  pthread::condition_variable _not_full_cv;
  pthread::condition_variable _not_empty_cv;

  std::list<T>            items ;
  atomic<int>             _max_size ;
};

// template implementation ------------------------------------------------

template<typename T> void sync_queue<T>::get( T& item, int wait_time ){
  pthread::lock_guard lck(mtx);

  if ( _not_empty_cv.wait_for(lck,wait_time, [this]{ return !items.empty(); })){
    item=items.front();
    items.pop_front();
    _not_full_cv.notify_one(); // signal queue is not full
  } else {
    throw sync_queue_timeout("queue is empty, sync_queue::get timedout.");
  }
}

template<typename T> void sync_queue<T>::put( const T& item, int wait_time ){
  pthread::lock_guard lck(mtx);

  if (_not_full_cv.wait_for(lck, wait_time, [this]{ return items.size() < _max_size; })){
    items.push_back(item);
    _not_empty_cv.notify_one(); // signal that an item was pushed in the queue, it's not empty anymore.
  } else {
    _not_full_cv.notify_all();
    throw sync_queue_full{"queue is full, sync_queue::put() timeout."};
  }
}

template<typename T> sync_queue<T>::sync_queue( int ms ): _max_size{ms}  {
}

template<typename T> sync_queue<T>::~sync_queue(){
}

using pthread::thread

to use pthread threads implement pthread::runnable interface :

class a_thread: public pthread::runnable {
public:
  void run() noexcept override{
    try {
   
    } catch ... ){
      // handle error
    }
  }
};

void main(){
  pthread::thread t1; // thread starts running immediatly
  pthread::thread t2;
  
  t1.join();
  t2.join();
}