16 thread_ptr =
new pthread_t();
19 delete_after_termination =
false;
26 delete_after_termination = _delete_after_termination;
29 pthread_attr_init(&attr);
30 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
31 if(pthread_create((pthread_t*)thread_ptr,&attr,execute_s,
this))
32 std::cerr <<
"error: starting thread" <<std::endl;
33 pthread_attr_destroy(&attr);
41 pthread_cond_wait(&(pthread_cond_t&)cm.pcond, &(pthread_mutex_t&)cm.pmutex);
52void* thread::execute_s(
void* args)
56 if (t->delete_after_termination)
72 Concurrency::wait(millisec);
74 usleep(1000*millisec);
82 pthread_join(*(pthread_t*)thread_ptr,NULL);
91 pthread_cancel(*(pthread_t*)thread_ptr);
101 pthread_join(*(pthread_t*)thread_ptr,NULL);
110 delete (pthread_t*)thread_ptr;
113thread_id_type to_id(
const pthread_t& pt)
115 return (
const thread_id_type&) pt;
121 return (
const thread_id_type&) pthread_self();
127 return *((
const thread_id_type*)thread_ptr);
133 void (*func)(thread_id_type);
146thread* start_in_thread(
void (*func)(thread_id_type),
bool _delete_after_termination)
148 thread* ft =
new function_thread(func);
149 ft->start(_delete_after_termination);
void run()
thread function to override
Thread class implementation that uses pthreads internally.
virtual void run()=0
thread function to override
static void wait(unsigned millisec)
wait the given number of milliseconds
void execute()
executes the run method
void stop()
try to stop the thread execution via indicating a stop request.
static void wait_for_signal(condition_mutex &cm)
sleep till the signal from the given condition_mutex is sent, lock the mutex first and unlock after w...
thread()
create the thread
static void wait_for_signal_with_lock(condition_mutex &cm)
prefered approach to wait for signal and implemented as { cm.lock(); wait_for_signal(cm); cm....
thread_id_type get_id() const
return id of this thread
void start(bool _delete_after_termination=false)
start the implemented run() method (asynchronly) and destruct the thread object
virtual ~thread()
standard destructor (a running thread will be killed)
void wait_for_completion()
the thread is interpreted as a slave thread and started from another master thread.
static thread_id_type get_current_thread_id()
return the id of the currently executed thread
void kill()
kill a running thread
A mutex that can wake up other threads by signals sent when a condition is fulfilled.
void unlock()
unlock the mutex
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...