红联Linux门户
Linux帮助

算法-多线程编程-pthread

发布时间:2005-09-22 11:31:49来源:红联作者:frog
/*******************************************************
Title : pthread-application.c
Author :
Time :
Function :
Comment :
Usage : 1、gcc -o pthread-application pthread-application.c -lpthread 2、./pthread-application
*******************************************************/


#include
#include
#include
#include "pthread.h"

void *thread_func(void *arg);
char message[]="hello";

int main(int argc,char *argv[])
{
int res;
pthread_t a_thread;
void *thread_result;

res=pthread_create(&a_thread,NULL,thread_func,(void *)message);
if(res!=0){
perror("Thread creation failed");
exit(EXIT_FAILURE);
}

printf("waiting for thread to finish....\n");
res=pthread_join(a_thread,&thread_result);
if(res!=0){
perror("thread join failed");
exit(EXIT_FAILURE);
}
printf("thread joined,it returned %s\n",(char *)thread_result);
printf("message is now %s\n",message);

exit(EXIT_SUCCESS);
}
void *thread_func(void *arg)
{
printf("thread_func is running .argument was %s\n",(char*) arg);
sleep(2);
strcpy(message,"bye");
pthread_exit("thank you for the cpu time");
}
文章评论

共有 6 条评论

  1. aslk12345 于 2010-12-08 17:53:29发表:

    Advanced Programing in Unix Envirement

  2. aslk12345 于 2010-12-08 17:50:01发表:

    软件开发者路线图:从学徒到高手

  3. yyxl 于 2010-12-08 15:55:40发表:

    站跳来跳去就是下不了
    看看这次能不能下

  4. 少华 于 2010-11-08 20:15:28发表:

    学习了

  5. yeqishi 于 2010-04-29 15:10:36发表:

    学习,顶

  6. yo 于 2005-09-27 00:43:26发表:

    支持