生活随笔
收集整理的這篇文章主要介紹了
线程终止
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
進程終止時exit()函數(shù),那么線程終止的是什么呢?
線程終止的三種情況:
線程只是從啟動函數(shù)中返回,返回的是線程的退出碼;線程可以被同一進程中的其他線程取消;線程調(diào)用pthread_exit。 /***
exit.c
***/
#include<stdio.h>
#include<
string.h>
#include<stdlib.h>
#include<errno.h>
#include<pthread.h>
void *func(
void *
arg)
{int i =
0;while(
1){if(
10 ==
i){int *p = (
int *)malloc(
sizeof(
int));*p =
11;pthread_exit(p);}printf("fun run %d \n",i++
);sleep(1);}return NULL;
}int main()
{pthread_t t1,t2;int err = pthread_create(&
t1,NULL,func,NULL);if(
0 !=
err){printf("thread_create failed : %s\n",strerror(errno));} else {printf("thread_create success\n");}void *p =
NULL;pthread_join(t1,&
p);printf("thread exit : code = %d\n",*(
int *
)p);return EXIT_SUCCESS;
} 運行結(jié)果:
exbot@ubuntu:~/wangqinghe/thread/20190729$ gcc exit.c -o exit -lpthread
exbot@ubuntu:~/wangqinghe/thread/20190729$ ./exit
thread_create success
fun run 0
fun run 1
fun run 2
fun run 3
fun run 4
fun run 5
fun run 6
fun run 7
fun run 8
fun run 9
thread exit : code = 11
?
void pthread_exit(void *arg)
pthread_exit函數(shù)的參數(shù)就跟正常線程結(jié)束return的使用時一樣的,都會被等待它結(jié)束的主線程獲取到。
轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11262731.html
總結(jié)
以上是生活随笔為你收集整理的线程终止的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。