算法-多线程编程-pthread

作者: frog 2005-09-22 11:31:49
/*******************************************************
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");
}

相关资讯