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);
}
[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);
}
相关文章推荐
- Linux之线程的创建、等待、终止及分离
- Linux-线程(一)创建 终止 等待线程
- Linux 线程(创建/等待/终止)
- Linux 线程(创建/等待/终止)
- [置顶] 【Linux】线程总结:初识、创建、等待、终止、分离
- 【Linux】线程(概念、特点、线程控制代码--创建/等待/终止、分离与结合属性)
- 嵌入式Linux应用程序开发——多线程1(线程的创建和等待)
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
- 线程创建与终止(Linux_C++)
- 线程的创建、终止、等待基本操作
- 线程创建、等待、终止
- 线程的控制(创建、等待、终止)、分离线程
- 线程的创建、终止、等待
- Linux 在线程中创建线程,在线程中等待线程
- linux下进程的状态,创建,替换,等待,终止
- Linux学习-线程的创建和终止-(出现错误-undefined reference to 'pthread_create')
- Linux多线程编程-线程的创建和终止
- linux基础编程:多线程基础总结:线程创建 终止 线程同步/互斥量/条件变量/信号灯
- 线程的创建,等待与终止