您的位置:首页 > 运维架构 > Linux

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

2014-11-12 14:51 253 查看
http://blog.csdn.net/hubi0952/article/details/7982487

[cpp]
view plaincopyprint?

/* 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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: