#include #include "simplethreading.h" // -------------- Simple 2-state semaphore ----------------------------- Mutex::Mutex() { int ret = pthread_mutex_init( &mutex, NULL); if (ret != 0) { fprintf(stderr, "mutex_init failed ret = %d\n",ret); } locked = false; } Mutex::~Mutex() { pthread_mutex_destroy(&mutex); } void Mutex::lock() { pthread_mutex_lock(&mutex); locked = true; } void Mutex::unlock() { pthread_mutex_unlock(&mutex); locked = false; } bool Mutex::isLocked() { return locked; } // -------------- Simple thread subclass, using pthreads ------------- SimpleThread::SimpleThread(void *data) { fprintf(stderr, "SimpleThread::SimpleThread\n"); userData = data; thread_id = 0; } SimpleThread::~SimpleThread() { stop(); } void *SimpleThread::boot(void *context) { fprintf(stderr, "SimpleThread::boot\n"); SimpleThread *st = (SimpleThread *) context; // instead of this st->run(); return(0); } void SimpleThread::start() { int n = pthread_create( &thread_id, NULL, boot, this); if(n) fprintf(stderr, "Error creating thread: %d\n", n); } void SimpleThread::stop() { if (pthread_equal(thread_id, 0)) { return; } void *status; pthread_join(thread_id, &status); //pthread_detach(msg_thread_id); // pthread_detach is implied in join. thread_id = 0; } bool SimpleThread::isRunning() { return ! pthread_equal(thread_id, 0); }