日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程挂起

發布時間:2023/11/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程挂起 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時候在一個線程中創建了另外一個線程,主線程要等到創建的線程返回了,獲取該線程的返回值后才退出,這個時候就需要把線程掛起。

?

int pthread_join(pthread_t th,void ** thr_return);

pthread_join函數用去掛起當前線程,直至th指定的線程終止為止。

/*** hangup.c ***/ #include<stdio.h> #include<pthread.h> #include<errno.h> #include<string.h> #include<stdlib.h>void * func(void *arg) {int i = 0;for(; i < 5; i++){printf("func run %d\n",i);sleep(1);}int *p = (int *)malloc(sizeof(int));*p = 11;return p; }int main() {pthread_t t1;int err = pthread_create(&t1,NULL,func,NULL);if( 0 != err){printf("thread_create failled : %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; }

運行結果:

exbot@ubuntu:~/wangqinghe/thread/20190729$ gcc hangup.c -o hangup -lpthread

exbot@ubuntu:~/wangqinghe/thread/20190729$ ./hangup

thread_create success

func run 0

func run 1

func run 2

func run 3

func run 4

thread exit : code = 11

?

主函數一直帶等待創建的線程執行完畢,并得到線程執行結束的返回值。

?

問題:

函數中malloc分配的是堆空間,如何返回個{}中的棧空間的。存疑。

轉載于:https://www.cnblogs.com/wanghao-boke/p/11262635.html

總結

以上是生活随笔為你收集整理的线程挂起的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。