#include #include #include #include #include #include #include #include static void* cliente(void *datos) { while(true) { //Allocating dummy memory of about 100K char *buff= new char[100000]; sleep(20); //releasing memory delete [] buff; buff=NULL; } } int main() { int ret1; ret1=0; pthread_t mythread[4]; while(ret1 < 3) { //create thread if(!pthread_create((pthread_t *)&mythread[ret1],NULL,cliente,NULL)) { perror("pthread_create"); } //dummy sleep sleep(60); //detach the thread int ret = pthread_detach((pthread_t)mythread[ret1]); if(EOK == ret) { printf("Thread %d got detached\n",mythread[ret1]); fflush(stdout); } //cancel the thread int iRet = pthread_cancel((pthread_t)mythread[ret1]); if(EOK == iRet) { printf("Thread %d got cancelled\n",mythread[ret1]); fflush(stdout); } mythread[ret1] = NULL; ret1++; } while(true) { } return EXIT_SUCCESS; }