05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
14
pithread.cpp
14
pithread.cpp
@@ -1,10 +1,17 @@
|
||||
#include "pithread.h"
|
||||
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay) {
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
running = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
timer = timer_delay;
|
||||
/*addEvent("started");
|
||||
addEvent("stopped");
|
||||
addEventHandler<int>(HANDLER(PIThread, start));
|
||||
addEventHandler(HANDLER(PIThread, startOnce));
|
||||
addEventHandler(HANDLER(PIThread, stop));
|
||||
addEventHandler<bool>(HANDLER(PIThread, terminate));*/
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
@@ -13,6 +20,7 @@ PIThread::PIThread(bool startNow, int timer_delay) {
|
||||
#endif
|
||||
|
||||
PIThread::~PIThread() {
|
||||
piMonitor.threads--;
|
||||
if (!running) return;
|
||||
#ifndef WINDOWS
|
||||
pthread_cancel(thread);
|
||||
@@ -79,12 +87,14 @@ void * PIThread::thread_function(void * t) {
|
||||
PIThread * ct = (PIThread * )t;
|
||||
ct->running = true;
|
||||
ct->begin();
|
||||
raiseEvent(ct, "started");
|
||||
while (!ct->terminating) {
|
||||
if (ct->lockRun) ct->mutex_.lock();
|
||||
ct->run();
|
||||
if (ct->lockRun) ct->mutex_.unlock();
|
||||
if (ct->timer > 0) msleep(ct->timer);
|
||||
}
|
||||
raiseEvent(ct, "stopped");
|
||||
ct->end();
|
||||
ct->running = false;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
@@ -101,9 +111,11 @@ void * PIThread::thread_function_once(void * t) {
|
||||
PIThread * ct = (PIThread * )t;
|
||||
ct->running = true;
|
||||
ct->begin();
|
||||
raiseEvent(ct, "started");
|
||||
if (ct->lockRun) ct->mutex_.lock();
|
||||
ct->run();
|
||||
if (ct->lockRun) ct->mutex_.unlock();
|
||||
raiseEvent(ct, "stopped");
|
||||
ct->end();
|
||||
ct->running = false;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
|
||||
Reference in New Issue
Block a user