Linux 线程(创设/等待/终止)

Linux 线程(创建/等待/终止)

/* FirstThreadFunc.c*/
#include <stdio.h>
//#include <unistd.h>
//#include <stdlib.h>
#include <pthread.h>
void
thread (void)
{
//  sleep(1);
  int i;
  int tid = pthread_self();//返回自己的线程ID
  for (i = 0; i < 3; i++)
    printf ("This is a pthread\n");
  pthread_exit("hello world!!\n");//把字符串的首地址传给thread_result
}

int
main (void)
{
  void *thread_result;
  pthread_t id;
  int i, res;
  res = pthread_create(&id, NULL, (void *) thread, NULL);//创建线程
  pthread_join(id, &thread_result);//等待id的线程结束
  if(res != 0)
    {
      printf ("Create pthread error!\n");
      exit (1);
    }
//    sleep(1);
  for (i = 0; i < 3; i++)
    printf ("This is the main process\n");

  printf("thread joined ,it returned %s\n",(char *)thread_result);
  return (0);
}

Linux 线程(创设/等待/终止)

相关文章:

你感兴趣的文章:

标签云: