linux c++(线程 & 杀死一个线程 & 线程分离 & 判断两个线程是否相同)

杀死线程
int pthread_cancel(pthread_t thread);
	需要传入pid
	返回值
		失败返回errno
		成功返回0
被pthread_cancel杀死的线程,退出状态为PTHREAD_CANCELED
强行设置取消点
pthread_testcancel();
比较两个线程ID是否相等
int pthread_equal(pthread_t t1,pthread_t t2);

线程id在进程内部是唯一的
void * thrFun(void *arg)
  {
      printf("I am thread = %d, tid=%lu\n",getpid(),pthread_self());
      sleep(4);
      printf("I am thread = %d, tid=%lu\n",getpid(),pthread_self());
      return NULL;
  }
  
  int main()
  {
      // 创建线程 #include <pthread.h>
      pthread_t tid;
      pthread_create(&tid,NULL,thrFun,NULL);    
      pthread_detach(tid); //使用线程分离
      sleep(5);                                                                                  
     int ret; 
    // 不需要再利用join回收了,不然会报错
     if ((ret = pthread_join(tid,NULL))>0) {
         printf("ret = %d,%s\n",ret,strerror(ret));
     }   
      return 0;
  }

posted on 2021-05-06 17:31  lodger47  阅读(545)  评论(0)    收藏  举报

导航