首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

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

2012-09-27 
Linux 线程(创建/等待/终止)/* FirstThreadFunc.c*/#include stdio.h//#include unistd.h//#include

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

/* FirstThreadFunc.c*/#include <stdio.h>//#include <unistd.h>//#include <stdlib.h>#include <pthread.h>voidthread (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}intmain (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);}

热点排行