线程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>

void *f(void *buf)
{
 char b[]="change";
 printf("buf in the created pthread:%s\n",(char *)buf);
 strncpy(buf, b, 6);//由于资源共享,这里的修改会导致buf中的内容在主线程中也改变
 pthread_exit("ok,the join to exit is success!");//线程退出
}

int main()

 pthread_t tid;
 void *result;
 char buf[]="test for the pthread\n";
 pthread_create(&tid, NULL, f, (void *)buf);//创建线程
 printf("buf in the last:%s\n",buf);
 pthread_join(tid, &result);//回收线程的资源
 printf("the return is : %s\n",(char *)result);
 return 0;
}

posted on 2012-04-08 17:18  小风儿_xf  阅读(131)  评论(0编辑  收藏  举报

导航