多執行緒 pthread join函式詳解

2021-05-25 05:19:42 字數 4297 閱讀 1949

from

單處理器上的linux多執行緒,是通過分時操作完成的;

此時互斥鎖的作用,只有在時間足夠的情況下才能體現出來,即有時執行緒內需要延時;

否則只有第乙個執行緒不斷解鎖和獲鎖,別的執行緒在第乙個執行緒執行完前無法獲得互斥鎖。

函式pthread_join用來等待乙個執行緒的結束。函式原型為:

extern int pthread_join __p ((pthread_t __th, void **__thread_return));

第乙個引數為被等待的執行緒識別符號,第二個引數為乙個使用者定義的指標,它可以用來儲存被等待執行緒的返回值。這個函式是乙個執行緒阻塞的函式,呼叫它的函式將一直等待到被等待的執行緒結束為止,當函式返回時,被等待執行緒的資源被收回。乙個執行緒的結束有兩種途徑,一種是象我們上面的例子一樣,函式結束了,呼叫它的執行緒也就結束了;另一種方式是通過函式pthread_exit來實現。它的函式原型為:

extern void pthread_exit __p ((void *__retval)) __attribute__ ((__noreturn__));

唯一的引數是函式的返回**,只要pthread_join中的第二個引數thread_return不是null,這個值將被傳遞給 thread_return。最後要說明的是,乙個執行緒不能被多個執行緒等待,否則第乙個接收到訊號的執行緒成功返回,其餘呼叫pthread_join的執行緒則返回錯誤**esrch。

在這一節裡,我們編寫了乙個最簡單的執行緒,並掌握了最常用的三個函式pthread_create,pthread_join和pthread_exit。下面,我們來了解執行緒的一些常用屬性以及如何設定這些屬性。

源程式:

/*thread_example.c :  c multiple thread programming in linux

#include

#include

#include

#include

#define max1 10

#define max2 30

pthread_t thread[2];

pthread_mutex_t mut;

int number=0, i;

void *thread1()

printf ("thread1 : i'm thread 1/n");

for (i = 0; i < max1; i++)

printf("thread1 : number = %d  i=%d/n",number,i);

pthread_mutex_lock(&mut);

number++;

pthread_mutex_unlock(&mut);

sleep(2);

printf("thread1 :is main function waiting for me acomplishing task? /n");

pthread_exit(null);

void *thread2()

printf("thread2 : i'm thread 2/n");

for (i = 0; i < max2; i++)

printf("thread2 : number = %d  i=%d/n",number,i);

pthread_mutex_lock(&mut);

number++;

pthread_mutex_unlock(&mut);

sleep(3);

printf("thread2 :is main function waiting for me  to acomplish task ?/n");

pthread_exit(null);

void thread_create(void)

int temp;

memset(&thread, 0, sizeof(thread));          //comment1

/*建立執行緒*/

if((temp = pthread_create(&thread[0], null, thread1, null)) != 0)       //comment2

printf("執行緒1建立失敗!/n");

else

printf("thread 1 is established/n");

if((temp = pthread_create(&thread[1], null, thread2, null)) != 0)  //comment3

printf("執行緒2建立失敗");

else

printf("thread 2 is established/n");

void thread_wait(void)

/*等待執行緒結束*/

if(thread[0] !=0)

if(thread[1] !=0)

int main()

/*用預設屬性初始化互斥鎖*/

pthread_mutex_init(&mut,null);

printf("i am the main funtion,and i am establishing threads. ha-ha/n");

thread_create();

printf("i am the main funtion,and i am waiting for thread to accomplish task. ha-ha/n");

thread_wait();

return 0;

執**況1(linux終端):[root@localhost root]# gcc -o joint joint.c -lpthread

[root@localhost root]# ./joint

i am the main funtion,and i am establishing threads. ha-ha

thread1 : i'm thread 1

thread1 : number = 0  i=0

thread 1 is established

thread2 : i'm thread 2

thread2 : number = 1  i=0

thread 2 is established

i am the main funtion,and i am waiting for thread to accomplish task. ha-ha

thread1 : number = 2  i=1

thread2 : number = 3  i=2

thread1 : number = 4  i=3

thread2 : number = 5  i=4

thread1 : number = 6  i=5

thread1 : number = 7  i=6

thread2 : number = 8  i=7

thread1 : number = 9  i=8

thread2 : number = 10  i=9

thread1 :is main function waiting for me acomplishing task?

thread 1 is over

thread2 : number = 11  i=11

thread2 : number = 12  i=12

thread2 : number = 13  i=13

thread2 : number = 14  i=14

thread2 : number = 15  i=15

thread2 : number = 16  i=16

thread2 : number = 17  i=17

thread2 : number = 18  i=18

thread2 : number = 19  i=19

thread2 : number = 20  i=20

thread2 : number = 21  i=21

thread2 : number = 22  i=22

thread2 : number = 23  i=23

thread2 : number = 24  i=24

thread2 : number = 25  i=25

thread2 : number = 26  i=26

thread2 : number = 27  i=27

thread2 : number = 28  i=28

thread2 : number = 29  i=29

thread2 :is main function waiting for me  to acomplish task ?

thread 2 is over

多執行緒 pthread join函式詳解

單處理器上的linux多執行緒,是通過分時操作完成的 此時互斥鎖的作用,只有在時間足夠的情況下才能體現出來,即有時執行緒內需要延時 否則只有第乙個執行緒不斷解鎖和獲鎖,別的執行緒在第乙個執行緒執行完前無法獲得互斥鎖。函式pthread join用來等待乙個執行緒的結束。函式原型為 extern in...

多執行緒pthread join 的作用

pthread join 函式原型 int pthread join pthread t thread,void retval args pthread t thread 被連線線程的執行緒號 void retval 指向乙個指向被連線線程的返回碼的指標的指標 引數 thread 執行緒識別符號,即...

多執行緒pthread join 的兩種作用

pthread join 函式原型 int pthread join pthread t thread,void retval args pthread t thread 被連線線程的執行緒號 void retval 指向乙個指向被連線線程的返回碼的指標的指標 return 執行緒連線的狀態,0是成...