线程分离

 

 

 

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

void *func(void *data)
{
  while (1)
  {
    printf("Speaking from the detached thread...\n");
    sleep(5);
  }
  pthread_exit(NULL);
}

int main()
{
  pthread_t handle;
  if (!pthread_create(&handle, NULL, func, NULL))
  {
    printf("Thread create successfully !!!\n");
    if (!pthread_detach(handle))
      printf("Thread detached successfully !!!\n");
  }

  sleep(5);
  printf("Main thread dying...\n");
  return 0;
}

输出:

posted @ 2019-05-10 15:24  苏格拉底的落泪  阅读(126)  评论(0编辑  收藏  举报