- morning panick victory
[The-Artvertiser.git] / artvertiser / FProfiler / FThread.h
blob6ad79d08335a9bc839a7e18be0fef656f2cc960e
1 /*
2 * FThread.h
3 * F
5 * Created by damian on 25/5/08.
6 * Copyright 2008 frey damian@frey.co.nz. All rights reserved.
8 */
10 #ifndef _THREAD_H
11 #define _THREAD_H
13 #include <pthread.h>
14 #include <assert.h>
15 #include <stdio.h>
17 /** base class for threads */
19 class FThread
21 public:
22 FThread( ) { thread_running = false; }
23 virtual ~FThread() { if ( thread_running ) StopThread(); }
25 /// start running the ThreadedFunction. thread_priority only takes effect if running as root.
26 void StartThread( int thread_priority = 0 ) ;
27 /// stop running ThreadedFunction
28 void StopThread();
30 protected:
32 /// override this to do what you want on this object
33 /// called repeatedly until the thread should die
34 virtual void ThreadedFunction() { printf("FThread::ThreadedFunction running.. override me!\n"); }
37 /// called internally
38 static void * run_function(void * objPtr){
39 FThread* the_fthread = (FThread*)objPtr;
41 while( !the_fthread->thread_should_stop )
42 the_fthread->ThreadedFunction();
44 the_fthread->thread_running = false;
46 pthread_exit(0);
47 return 0;
50 pthread_t the_thread;
51 bool thread_running;
52 bool thread_should_stop;
56 class FThreadContext
58 public:
59 FThreadContext() { Set(); }
61 /// set to the current thread context
62 void Set() { context = pthread_self(); }
64 /// equal?
65 bool operator == ( const FThreadContext& other ) const { return pthread_equal( context, other.context ); }
67 private:
68 pthread_t context;
71 #endif