|
cpp-pthread
(v1.7.3)
Simple C++ wrapper to pthread functions.
|
#include <thread.hpp>
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 |
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();
}
Definition at line 241 of file thread.hpp.
|
explicit |
setup thread base.
| stack_size | thread's stack size (default 0 which means use PTHREAD_STACK_MIN) |
Definition at line 132 of file thread.cpp.
| void pthread::abstract_thread::join | ( | ) |
joins this thread.
| pthread_exception | if deadlock conditions are detected. |
Definition at line 147 of file thread.cpp.
| bool pthread::abstract_thread::joinable | ( | ) | const |
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.
1.8.8