cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Public Member Functions | List of all members
pthread::abstract_thread Class Reference

#include <thread.hpp>

Inheritance diagram for pthread::abstract_thread:
pthread::runnable

Public Member Functions

 abstract_thread (const std::size_t stack_size=0)
 
void start ()
 
void join ()
 
bool joinable () const
 
- Public Member Functions inherited from pthread::runnable
virtual void run () noexcept=0
 

Detailed Description

base class of a thread.

utility class, that wraps a thread.


class worker: public pthread::abstract_thread {
public:
  worker(const std::string m = "anonymous worker", int sleep = 2*1000): msg(m), _sleep(sleep){
  };
  ~worker(){
  };
  void run() noexcept override {
    { // critical section scope
      pthread::lock_guard<pthread::mutex> lck(mtx);
      bool stop_waiting = true; // if lambda syntax is not availbale then use this kind of implementation
      auto delay = _sleep; // use sleep seconds to calculate point in time timeou
      while ( ! (stop_waiting = (counter >= 10000)) && (condition.wait_for(mtx, delay) == pthread::cv_status::no_timeout)){
        delay = -1 ; // if timeout millis is negatif, then we keep last timeout calculation.
      }
      if ( counter >= 10000 ) {
        message("worker class, counter >= 10000");
      } else {
        message("worker class, counter < 10000");
      }
    } // end of critical section
    pthread::this_thread::sleep_for(200);
  };
private:
  std::string    msg ;
  int            _sleep;
};
int main(int argc, const char * argv[]) {
  pthread::thread_group threads(true); // indicate that we want to join referenced threads when deallocating this instance.
  for (auto x = 10 ; x > 0 ; x--){
    threads.add( new worker("herbert"));
  }
  threads.start(); // start running all threads
  for ( auto x = 20000 ; x > 0 ; x--){
    pthread::lock_guard<pthread::mutex> lck(mtx);
    counter++ ;
  }
  condition.notify_all();
}
Author
herbert koelman (herbe.nosp@m.rt.k.nosp@m.oelma.nosp@m.n@me.nosp@m..com)
Examples:
synchronized_queue_tests.cpp.

Definition at line 241 of file thread.hpp.

Constructor & Destructor Documentation

pthread::abstract_thread::abstract_thread ( const std::size_t  stack_size = 0)
explicit

setup thread base.

Parameters
stack_sizethread's stack size (default 0 which means use PTHREAD_STACK_MIN)

Definition at line 132 of file thread.cpp.

Member Function Documentation

void pthread::abstract_thread::join ( )

joins this thread.

Exceptions
pthread_exceptionif deadlock conditions are detected.

Definition at line 147 of file thread.cpp.

bool pthread::abstract_thread::joinable ( ) const
Returns
true if this thread can be joined.

Definition at line 151 of file thread.cpp.

void pthread::abstract_thread::start ( )

start running the run() method in a new thread.

Definition at line 142 of file thread.cpp.


The documentation for this class was generated from the following files: