/* Programming with pthread: cancelability state: pthread2.c++ $ g++ -Wall pthread2.c++ -lpthread $ ./a.out */ #include using namespace std; #include #include #define MAXLOOP 15 void * thread(void *) ; int tS, *tSP; int main() { pthread_t tid; // pthread2.c++ pthread_create(&tid, NULL, thread, NULL); pthread_cancel(tid); pthread_join(tid, (void **)&tSP); // cout << "Thread status: " << *tSP << endl; return 0 ; } void *thread(void *vp) { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); for(int i=1; i<= MAXLOOP; ++i) { sleep(1); if(i==10) pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL); cout << "Thread Running: " << i << endl; } tS = 1; pthread_exit((void *)&tS) ; }