代码:
注意线程的三种终止方法。不能直接exit,这样会终止整个程序。
Makefile(注意多线程的编译要加 -lpthread)
![](/attachment/201607/14/30154485_1468462744jJFa.png)
运行结果:
可以看到主线程终止了新创建的线程,等待成功。
看一下所用到的函数
1.创建一个线程
On success, pthread_create() returns 0;
on error, it returns an error number, and the contents of *thread are undefined.
![](/attachment/201607/14/30154485_1468463866tCnL.png)
2.线程等待
On success, pthread_join() returns 0;
on error, it returns an error number.
![](/attachment/201607/14/30154485_1468463967vst5.png)
3.线程分离
On success, pthread_detach() returns 0; on error, it returns an error number
![](/attachment/201607/14/30154485_1468464083HMcz.png)
4.线程退出
(比较简单就不截图了)
pthread_cancel(id)
pthread_eixt(void* status)
5.线程ID
pthread_self(void)
阅读(1393) | 评论(0) | 转发(0) |