#include"Thread.h" #include<signal.h> friend void* runThread(void*arg){return ((IThread*)arg)->run();}; //implemented in .h file IThread::IThread() { m_tid = 0; m_running = -1; m_detached = 0; // signal(SIGPIPE,SIG_IGN); } /*static void* runThread(void* arg) { }*/ IThread::~IThread() { if (m_running == 1 && m_detached == 0) { printf("\n deaached from thread %lu \n",m_tid); pthread_detach(m_tid); } if (m_running == 1) { printf("\n cancel from thread %lu \n",m_tid); pthread_cancel(m_tid); pthread_attr_destroy(&attr); } } int IThread::start() { pthread_attr_t attr; int g = pthread_attr_init(&attr); if(g!=0) printf("\nfail attr\n"); pthread_attr_setstacksize(&attr,2000*1000); int res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); int result = pthread_create(&m_tid, &attr, runThread, this); if (result == 0) { m_running = 1; } return result; } int IThread::join() { int result = -1; if (m_running == 1) { result = pthread_join(m_tid, NULL); if (result == 0) { m_detached = 1; } } return result; } int IThread::detach() { int result = -1; if (m_running == 1 && m_detached == 0) { result = pthread_detach(m_tid); if (result == 0) { m_detached = 1; } } return result; } pthread_t IThread::self() { return m_tid; } void* IThread::run() { printf("Empty thread"); return 0; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)