pthread create 函式的安全使用

2021-05-12 12:35:25 字數 4644 閱讀 9271

1:pthread_create 函式的安全使用問題

做過linux多執行緒開發的人都會用過pthread_create函式,但是很少人會注意到:主線程在使用pthread_create建立執行緒時,如果pthread_create 函式的第四個引數(指標引數)傳入的值會被主線程隨時修改時,這時我們的執行緒從pthread_create 函式的第四個引數獲取的值可能就不是我們想要傳入的值了。

看看下面**:在 main 函式的 for 迴圈中建立10個執行緒;每建立乙個執行緒時都把迴圈變數 i 的值通過 pthread_create 函式的第四個引數傳入執行緒函式中;但是執行緒獲取的值可能不並不是我們想傳入的值。

#include

#include

void * threadfunc( void * pm_ipthreadid )}}

int main()

}while(1)

return 0;

}程式執行結果如下(結果是隨機的,每次執行可能都不一樣):

@@@ create no.0 pthread success; pthreadid = 138174472 !

### no.1 pthread start exec; pthread id = 3076803472 !

### no.1 pthread start exec; pthread id = 3087293328 !

@@@ create no.1 pthread success; pthreadid = 138174476 !

@@@ create no.2 pthread success; pthreadid = 138174480 !

### no.3 pthread start exec; pthread id = 3055823760 !

### no.3 pthread start exec; pthread id = 3066313616 !

@@@ create no.3 pthread success; pthreadid = 138174484 !

### no.4 pthread start exec; pthread id = 3045333904 !

@@@ create no.4 pthread success; pthreadid = 138174488 !

@@@ create no.5 pthread success; pthreadid = 138174492 !

### no.6 pthread start exec; pthread id = 3024354192 !

### no.6 pthread start exec; pthread id = 3034844048 !

@@@ create no.6 pthread success; pthreadid = 138174496 !

@@@ create no.7 pthread success; pthreadid = 138174500 !

### no.8 pthread start exec; pthread id = 3003374480 !

### no.8 pthread start exec; pthread id = 3013864336 !

@@@ create no.8 pthread success; pthreadid = 138174504 !

@@@ create no.9 pthread success; pthreadid = 138174508 !

### no.10 pthread start exec; pthread id = 2992884624 !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

從以「###」開頭的字元結果可以看出:以「###no.0」、「###no.2」、「###no.5、「###no.7」、「###no.7」 的字元沒有;從這可以看出,我們從pthread_create 函式的第四個引數傳入的值是不安全的,應該受到安全保護。

2:此問題原因

pthread_create 函式是非阻塞函式;會立即返回,主線程不會在建立執行緒後阻塞到執行緒函式執行完後返回,導致執行緒函式獲取到迴圈變數 i 的值被主線程立即改變。

3:此問題解決方法

#include

#include

pthread_cond_t m_cond;

pthread_mutex_t m_locker;

void * threadfunc( void * pm_ipthreadid )}}

int main()

pthread_cond_wait(&m_cond, &m_locker);

pthread_mutex_unlock(&m_locker);

}while(1)

return 0;

}上面**執行結果:

@@@ create no.0 pthread success; pthreadid = 147902472 !

### no.0 pthread start exec; pthread id = 3087563664 !

@@@ create no.1 pthread success; pthreadid = 147902476 !

### no.1 pthread start exec; pthread id = 3077073808 !

@@@ create no.2 pthread success; pthreadid = 147902480 !

### no.2 pthread start exec; pthread id = 3066583952 !

@@@ create no.3 pthread success; pthreadid = 147902484 !

### no.3 pthread start exec; pthread id = 3056094096 !

@@@ create no.4 pthread success; pthreadid = 147902488 !

### no.4 pthread start exec; pthread id = 3045604240 !

@@@ create no.5 pthread success; pthreadid = 147902492 !

### no.5 pthread start exec; pthread id = 3035114384 !

@@@ create no.6 pthread success; pthreadid = 147902496 !

### no.6 pthread start exec; pthread id = 3024624528 !

@@@ create no.7 pthread success; pthreadid = 147902500 !

### no.7 pthread start exec; pthread id = 3014134672 !

@@@ create no.8 pthread success; pthreadid = 147902504 !

### no.8 pthread start exec; pthread id = 3003644816 !

@@@ create no.9 pthread success; pthreadid = 147902508 !

### no.9 pthread start exec; pthread id = 2993154960 !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

$$$ no.10 pthread cirle exec !

pthread create 函式用法

天開始學習linux下用c開發多執行緒程式,linux系統下的多執行緒遵循posix執行緒介面,稱為pthread。include int pthread create pthread t restrict tidp,const pthread attr t restrict attr,void s...

pthread create函式詳解

pthread create是unix環境建立執行緒函式 include int pthread create pthread t restrict tidp,const pthread attr t restrict attr,void start rtn void void restrict a...

執行緒建立函式pthread create

標頭檔案 include b函式原型 int pthread created pthread t thread,pthread attr t attr,void start routine void void arg 函式引數含義 thread 該引數是乙個指標,當執行緒建立成功時,用來返回建立的執...