// last modified or checked 12 August 2002

#include <pthread.h>


#ifndef _REENTRANT 
	#define _REEENTRANT
#endif

// IMPORTANT INFORMATION

// NEVER CREATE AN INSTANCE OF A THREAD ON THE STACK (local memory), ALWAYS CREATE
// A NEW INSTANCE USING new.
// THIS MEMORY IS THEN HELD BY THE THREAD TO MANAGE, SO MEMORY MANAGEMENT (in regards to 
// the thread) IS NOT NEEDED AFTER CREATION OF THIS THREAD
// THREADS CANNOT BE DIRECTLY INSTANTIATED, BUT MUST BE INHERITED BY ANOTHER CLASS
// WHICH MUST OVERRIDE THE void run(), VIRTUAL FUNCTION MEMBER



#ifndef _THREAD
#define _THREAD


extern "C" {	// need this external static function for archaic C-Style function requirements

	void*	StartThread(void* arg);

};



class Thread{


	public:

		virtual ~Thread()=0;

		void start();  // place where the thread really starts.. call this when make thread (like java Thread class)

		virtual void run()=0; // implement this function as the point where the thread starts

	private:

		pthread_t id;
		pthread_attr_t attr;
};


Thread::~Thread(){}



void Thread::start(){

	pthread_attr_init(&attr);
	pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM); 

	pthread_create(&id, NULL, StartThread, (void*)this);  // use StartThread(client)
}



extern "C" {

	void*	StartThread(void* arg){
		
		Thread* athread = (Thread*) arg;
		athread->run();
		delete athread;
		return NULL;
	}
};


#endif	// end of #define _THREAD
