root / tbeta / Windows / addons / ofxThread / src / ofxThread.h @ 2
View | Annotate | Download (1.2 KB)
| 1 | #ifndef _OFX_THREAD_H_
|
|---|---|
| 2 | #define _OFX_THREAD_H_
|
| 3 | |
| 4 | #include "ofConstants.h" |
| 5 | |
| 6 | #ifdef TARGET_WIN32
|
| 7 | #include <process.h> |
| 8 | #else
|
| 9 | #include <pthread.h> |
| 10 | #include <semaphore.h> |
| 11 | #endif
|
| 12 | |
| 13 | class ofxThread{
|
| 14 | |
| 15 | public:
|
| 16 | ofxThread(); |
| 17 | virtual ~ofxThread(); |
| 18 | bool isThreadRunning();
|
| 19 | void startThread(bool _blocking = true, bool _verbose = true); |
| 20 | bool lock();
|
| 21 | bool unlock();
|
| 22 | void stopThread();
|
| 23 | |
| 24 | protected:
|
| 25 | |
| 26 | //-------------------------------------------------
|
| 27 | //you need to overide this with the function you want to thread
|
| 28 | virtual void threadedFunction(){
|
| 29 | if(verbose)printf("ofxThread: overide threadedFunction with your own\n"); |
| 30 | } |
| 31 | |
| 32 | //-------------------------------------------------
|
| 33 | |
| 34 | #ifdef TARGET_WIN32
|
| 35 | static unsigned int __stdcall thread(void * objPtr){ |
| 36 | ofxThread* me = (ofxThread*)objPtr; |
| 37 | me->threadedFunction(); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | #else
|
| 42 | static void * thread(void * objPtr){ |
| 43 | ofxThread* me = (ofxThread*)objPtr; |
| 44 | me->threadedFunction(); |
| 45 | return 0; |
| 46 | } |
| 47 | #endif
|
| 48 | |
| 49 | |
| 50 | #ifdef TARGET_WIN32
|
| 51 | HANDLE myThread; |
| 52 | CRITICAL_SECTION critSec; //same as a mutex
|
| 53 | #else
|
| 54 | pthread_t myThread; |
| 55 | pthread_mutex_t myMutex; |
| 56 | #endif
|
| 57 | |
| 58 | bool threadRunning;
|
| 59 | bool locked;
|
| 60 | bool blocking;
|
| 61 | bool verbose;
|
| 62 | }; |
| 63 | |
| 64 | #endif
|
