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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

UNIX环境高级编程——pthread_create的问题

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UNIX环境高级编程——pthread_create的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ?linux 下常用的創建多線程函數pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)(void*) , void *args);

其中第一個參數用來保存線程信息,第二個參數指新線程的運行屬性,可以設置為NULL,第三個參數為自定義的線程函數,第四個參數就是線程函數需要用到的參數,一般如果要傳遞多個參數,可以設置為結構體(struct)類型,這里我們使用int類型的變量。 ?

? ? ?下面我著重討論一個用for結構來創建多個線程時參數傳遞的問題:

#include <stdio.h> #include <stdlib.h> #include <pthread.h>#define th_pop 20 pthread_mutex_t mutex;pthread_t a_thread[th_pop];void * thread_func(void *args) {pthread_mutex_lock(&mutex);int t_id = *(int*)args;printf("the id of this thread is %d\n",t_id);pthread_mutex_unlock(&mutex); return (void*)NULL; }void init() {pthread_mutex_init(&mutex, NULL);int i;for( i=0; i<th_pop; i++){pthread_create(&a_thread[i] , NULL , thread_func , &i);}//wait the end of the threads;for( i=0; i<th_pop; i++){int res = pthread_join(a_thread[i] , NULL);if(res != 0)printf("the thread id: %d ends fail \n",i);}pthread_mutex_destroy(&mutex);}int main() {init();return 0; }

運行結果:

huangcheng@ubuntu:~$ ./a.out the id of this thread is 2 the id of this thread is 8 the id of this thread is 9 the id of this thread is 9 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20 the id of this thread is 20

看到這個結果有沒有感覺到有什么不對呢?可能你會感覺到很納悶,怎么出現了那么多的id=0的結果呢?其實這個認真分析一下并不難理解:

首先pthread_create函數傳遞的是一個指針型的參數,即傳遞的是一個地址而已,這樣在執行for結構時:

for(int i=0; i<th_pop; i++) {pthread_create(&a_thread[i] , NULL , thread_func , &i); }


? ? ?該for循環快速執行完成,并且將i置為20,故而傳遞的地址指向的內容為20,同時其它的線程還沒來得及執行:
?int t_id = *(int*)args;

這樣就使得多個線程都指向同一個地址,內容為20,解決該問題的一個辦法為中for結構中加入sleep(1),這樣當sleep時間大于線程函數執行時間,就可以得到一個正確的結果,不過這種辦法剝掉了并發性,并不可取,下面我們采用另一種方法。

我們只修改init()函數

#include <stdio.h> #include <stdlib.h> #include <pthread.h>#define th_pop 20 //pthread_mutex_t mutex;pthread_t a_thread[th_pop];void * thread_func(void *args) {pthread_mutex_lock(&mutex);int t_id = *(int*)args;printf("the id of this thread is %d\n",t_id);pthread_mutex_unlock(&mutex); return (void*)NULL; }void init() {pthread_mutex_init(&mutex, NULL);int thread_id[th_pop];int i;for( i=0; i<th_pop; i++)thread_id[i] = i;for( i=0; i<th_pop; i++){int *t = thread_id +i;pthread_create(&a_thread[i] , NULL , thread_func , (void*)t);}//wait the end of the threads;for( i=0; i<th_pop; i++){int res = pthread_join(a_thread[i] , NULL);if(res != 0)printf("the thread id: %d ends fail \n",i);}pthread_mutex_destroy(&mutex);}int main() {init();return 0; }運行結果:

huangcheng@ubuntu:~$ ./a.out the id of this thread is 3 the id of this thread is 2 the id of this thread is 1 the id of this thread is 4 the id of this thread is 5 the id of this thread is 6 the id of this thread is 7 the id of this thread is 8 the id of this thread is 9 the id of this thread is 10 the id of this thread is 11 the id of this thread is 12 the id of this thread is 13 the id of this thread is 14 the id of this thread is 15 the id of this thread is 16 the id of this thread is 17 the id of this thread is 18 the id of this thread is 19 the id of this thread is 0
從這個例子中我們應該明白,要避免直接在傳遞的參數中傳遞發生改變的量,否則會導致結果不可測。

解決這類問題的辦法:

(1)復制一份到堆里面。
(2)加鎖。(一般做法)
(3)用結構體數組。(推薦這個)
(4)sleep。(不推薦)


轉載于:https://www.cnblogs.com/wangfengju/p/6172684.html

總結

以上是生活随笔為你收集整理的UNIX环境高级编程——pthread_create的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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