#include <pthread.h>
#include <iostream>
#include <unistd.h>

class Thread {
	private:
		pthread_t thread;
	public:
		Thread(){
		}
	
		~Thread(){
			this->exitThread();
			delete this;
		}

		pthread_t* initThread(){
			if(!pthread_create(&(this->thread),NULL,Thread::startThread,static_cast<void *>(this))){
// 				std::cerr << "- THREAD CREATION" << std::endl;
				return &(this->thread);
			} else {
// 				std::cerr << "- ERROR in pthread_create()" << std::endl;
				exit(1);
			}
		}
	
		static void* startThread(void* thread) {
			static_cast<Thread *>(thread)->createThread();
			pthread_exit(0);
		}
	
		virtual void createThread() = 0;


		pthread_t* getThread(){
			return &(this->thread);
		}

		void cancelThread(){
			pthread_cancel(this->thread);
		}

		void waitThread(){
			pthread_join(this->thread,NULL);
		}

		void exitThread(){
			pthread_exit(0);
		}

};

