/***************************************************** ** Thread.cpp ** ** ---------- ** ** ** ** A very simple thread class built on top of the ** ** pthread library. It allows you to create a ** ** new thread that starts execution from an ** ** aplication defined entry point with a ** ** function prototype of the form: ** ** void *MyEntryFunction( void *argument); ** ** Also implemented are very basic thread query ** ** operations to determine if it is currently ** ** running (IsRunning()) and to check the ** ** application-defined threadID (GetThreadID()). ** ** A function to kill the thread externally is ** ** implemented via Kill(), though this is not ** ** particularly robust, given that no signal ** ** handling is defined. ** ** ** ** ** ** Chris Wyman (2/12/2007) ** *****************************************************/ #include #include #include "Thread.h" // This function spawns a new thread, which starts execution // inside the function specified by 'entryPoint'. The single // argument passed to the entryPoint will be a pointer to this Thread. bool Thread::Create( void *(*entryPoint)(void *) ) { // return Create( entryPoint, this ); // Actually create the thread. int ret = pthread_create( &threadData, NULL, entryPoint, (void *)this ); // If ret value != 0, there was an error with pthread_create() if (ret != 0) isRunning = false; // Return a value that specifies if the thread was created successfully. return isRunning; } // This function terminates execution of the this thread. It should only be // called from inside this thread. If another thread wishes to terminate this // one, it should instead call Thread::Kill(). void Thread::Exit( void ) { isRunning = false; pthread_exit( NULL ); } // This function allows another thread to terminate this thread. If a thread // wants to terminate itself, it should either call Thread::Exit(), or simply // return from the function specified by Create() (similar to how returning // from main() exits a program.) int Thread::Kill( int signal ) { return pthread_kill( threadData, signal ); }