C語言線程庫的使用
一、線程概述
線程是輕量級的進(jìn)程(LWP:light weight process),在 Linux 環(huán)境下線程的本質(zhì)仍是進(jìn)程。在計算機(jī)上運(yùn)行的程序是一組指令及指令參數(shù)的組合,指令按照既定的邏輯控制計算機(jī)運(yùn)行。操作系統(tǒng)會以進(jìn)程為單位,分配系統(tǒng)資源,可以這樣理解,進(jìn)程是資源分配的最小單位,線程是操作系統(tǒng)調(diào)度執(zhí)行的最小單位。
先從概念上了解一下線程和進(jìn)程之間的區(qū)別:
1. 進(jìn)程有自己獨立的地址空間,多個線程共用同一個地址空間
- 線程更加節(jié)省系統(tǒng)資源,效率不僅可以保持的,而且能夠更高
- 在一個地址空間中多個線程獨享:每個線程都有屬于自己的棧區(qū),寄存器 (內(nèi)核中管理的)
- 在一個地址空間中多個線程共享:代碼段,堆區(qū),全局?jǐn)?shù)據(jù)區(qū),打開的文件 (文件描述符表) 都是線程共享的
2. 線程是程序的最小執(zhí)行單位,進(jìn)程是操作系統(tǒng)中最小的資源分配單位
- 每個進(jìn)程對應(yīng)一個虛擬地址空間,一個進(jìn)程只能搶一個 CPU 時間片
- 一個地址空間中可以劃分出多個線程,在有效的資源基礎(chǔ)上,能夠搶更多的 CPU 時間片
3. CPU 的調(diào)度和切換:線程的上下文切換比進(jìn)程要快的多
上下文切換:進(jìn)程 / 線程分時復(fù)用 CPU 時間片,在切換之前會將上一個任務(wù)的狀態(tài)進(jìn)行保存,下次切換回這個任務(wù)的時候,加載這個狀態(tài)繼續(xù)運(yùn)行,任務(wù)從保存到再次加載這個過程就是一次上下文切換。
4. 線程更加廉價,啟動速度更快,退出也快,對系統(tǒng)資源的沖擊小。
在處理多任務(wù)程序的時候使用多線程比使用多進(jìn)程要更有優(yōu)勢,但是線程并不是越多越好,如何控制線程的個數(shù)呢?
- 文件 IO 操作:文件 IO 對 CPU 是使用率不高,因此可以分時復(fù)用 CPU 時間片,線程的個數(shù) = 2 * CPU 核心數(shù) (效率最高)
- 處理復(fù)雜的算法 (主要是 CPU 進(jìn)行運(yùn)算,壓力大),線程的個數(shù) = CPU 的核心數(shù) (效率最高)
二、創(chuàng)建線程
2.1 線程函數(shù)
每一個線程都有一個唯一的線程 ID,ID 類型為 pthread_t,這個 ID 是一個無符號長整形數(shù),如果想要得到當(dāng)前線程的線程 ID,可以調(diào)用如下函數(shù):
- pthread_t pthread_self(void); // 返回當(dāng)前線程的線程ID
在一個進(jìn)程中調(diào)用線程創(chuàng)建函數(shù),就可得到一個子線程,和進(jìn)程不同,需要給每一個創(chuàng)建出的線程指定一個處理函數(shù),否則這個線程無法工作。
- #include <pthread.h>
- int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
- void *(*start_routine) (void *), void *arg);
- // Compile and link with -pthread, 線程庫的名字叫pthread, 全名: libpthread.so libptread.a
參數(shù):
- thread: 傳出參數(shù),是無符號長整形數(shù),線程創(chuàng)建成功,會將線程 ID 寫入到這個指針指向的內(nèi)存中
- attr: 線程的屬性,一般情況下使用默認(rèn)屬性即可,寫 NULL
- start_routine: 函數(shù)指針,創(chuàng)建出的子線程的處理動作,也就是該函數(shù)在子線程中執(zhí)行。
- arg: 作為實參傳遞到 start_routine 指針指向的函數(shù)內(nèi)部
返回值:線程創(chuàng)建成功返回 0,創(chuàng)建失敗返回對應(yīng)的錯誤號
2.2 創(chuàng)建線程
下面是創(chuàng)建線程的示例代碼,在創(chuàng)建過程中一定要保證編寫的線程函數(shù)與規(guī)定的函數(shù)指針類型一致:void *(*start_routine) (void *):
- // pthread_create.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 子線程的處理代碼
- void* working(void* arg)
- {
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf("child == i: = %d\n", i);
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 休息, 休息一會兒...
- // sleep(1);
- return 0;
- }
編譯測試程序,會看到如下錯誤信息:
- $ gcc pthread_create.c
- /tmp/cctkubA6.o: In function `main':
- pthread_create.c:(.text+0x7f): undefined reference to `pthread_create'
- collect2: error: ld returned 1 exit status
錯誤原因是因為編譯器鏈接不到線程庫文件(動態(tài)庫),需要在編譯的時候通過參數(shù)指定出來,動態(tài)庫名為 libpthread.so 需要使用的參數(shù)為 -l,根據(jù)規(guī)則掐頭去尾最終形態(tài)應(yīng)該寫成:-lpthread(參數(shù)和參數(shù)值中間可以有空格)。正確的編譯命令為:
- # pthread_create 函數(shù)的定義在某一個庫中, 編譯的時候需要加庫名 pthread
- $ gcc pthread_create.c -lpthread
- $ ./a.out
- 子線程創(chuàng)建成功, 線程ID: 139712560109312
- 我是主線程, 線程ID: 139712568477440
- i = 0
- i = 1
- i = 2
在打印的日志輸出中為什么子線程處理函數(shù)沒有執(zhí)行完畢呢(只看到了子線程的部分日志輸出)?
主線程一直在運(yùn)行,執(zhí)行期間創(chuàng)建出了子線程,說明主線程有 CPU 時間片,在這個時間片內(nèi)將代碼執(zhí)行完畢了,主線程就退出了。子線程被創(chuàng)建出來之后需要搶 cpu 時間片, 搶不到就不能運(yùn)行,如果主線程退出了, 虛擬地址空間就被釋放了, 子線程就一并被銷毀了。但是如果某一個子線程退出了, 主線程仍在運(yùn)行, 虛擬地址空間依舊存在。
得到的結(jié)論:在沒有人為干預(yù)的情況下,虛擬地址空間的生命周期和主線程是一樣的,與子線程無關(guān)。
目前的解決方案:讓子線程執(zhí)行完畢,主線程再退出,可以在主線程中添加掛起函數(shù) sleep();
三、線程退出
在編寫多線程程序的時候,如果想要讓線程退出,但是不會導(dǎo)致虛擬地址空間的釋放(針對于主線程),我們就可以調(diào)用線程庫中的線程退出函數(shù),只要調(diào)用該函數(shù)當(dāng)前線程就馬上退出了,并且不會影響到其他線程的正常運(yùn)行,不管是在子線程或者主線程中都可以使用。
- #include <pthread.h>
- void pthread_exit(void *retval);
- 參數(shù):線程退出的時候攜帶的數(shù)據(jù),當(dāng)前子線程的主線程會得到該數(shù)據(jù)。如果不需要使用,指定為 NULL
下面是線程退出的示例代碼,可以在任意線程的需要的位置調(diào)用該函數(shù):
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 子線程的處理代碼
- void* working(void* arg)
- {
- sleep(1);
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- if(i==6)
- {
- pthread_exit(NULL); // 直接退出子線程
- }
- printf("child == i: = %d\n", i);
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 主線程調(diào)用退出函數(shù)退出, 地址空間不會被釋放
- pthread_exit(NULL);
- return 0;
- }
四、線程回收
4.1 線程函數(shù)
線程和進(jìn)程一樣,子線程退出的時候其內(nèi)核資源主要由主線程回收,線程庫中提供的線程回收函叫做 pthread_join(),這個函數(shù)是一個阻塞函數(shù),如果還有子線程在運(yùn)行,調(diào)用該函數(shù)就會阻塞,子線程退出函數(shù)解除阻塞進(jìn)行資源的回收,函數(shù)被調(diào)用一次,只能回收一個子線程,如果有多個子線程則需要循環(huán)進(jìn)行回收。
另外通過線程回收函數(shù)還可以獲取到子線程退出時傳遞出來的數(shù)據(jù),函數(shù)原型如下:
- #include <pthread.h>
- // 這是一個阻塞函數(shù), 子線程在運(yùn)行這個函數(shù)就阻塞
- // 子線程退出, 函數(shù)解除阻塞, 回收對應(yīng)的子線程資源, 類似于回收進(jìn)程使用的函數(shù) wait()
- int pthread_join(pthread_t thread, void **retval);
參數(shù):
- thread: 要被回收的子線程的線程 ID
- retval: 二級指針,指向一級指針的地址,是一個傳出參數(shù),這個地址中存儲了 pthread_exit () 傳遞出的數(shù)據(jù),如果不需要這個參數(shù),可以指定為 NULL
返回值:線程回收成功返回 0,回收失敗返回錯誤號。
4.2 回收子線程數(shù)據(jù)
在子線程退出的時候可以使用 pthread_exit() 的參數(shù)將數(shù)據(jù)傳出,在回收這個子線程的時候可以通過 phread_join() 的第二個參數(shù)來接收子線程傳遞出的數(shù)據(jù)。接收數(shù)據(jù)有很多種處理方式,下面來列舉幾種:
4.2.1 使用子線程棧通過函數(shù) pthread_exit(void *retval); 可以得知,子線程退出的時候,需要將數(shù)據(jù)記錄到一塊內(nèi)存中,通過參數(shù)傳出的是存儲數(shù)據(jù)的內(nèi)存的地址,而不是具體數(shù)據(jù),由因為參數(shù)是 void* 類型,所有這個萬能指針可以指向任意類型的內(nèi)存地址。先來看第一種方式,將子線程退出數(shù)據(jù)保存在子線程自己的棧區(qū):
- // pthread_join.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 定義結(jié)構(gòu)
- struct Persion
- {
- int id;
- char name[36];
- int age;
- };
- // 子線程的處理代碼
- void* working(void* arg)
- {
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf("child == i: = %d\n", i);
- if(i == 6)
- {
- struct Persion p;
- p.age =12;
- strcpy(p.name, "tom");
- p.id = 100;
- // 該函數(shù)的參數(shù)將這個地址傳遞給了主線程的pthread_join()
- pthread_exit(&p);
- }
- }
- return NULL; // 代碼執(zhí)行不到這個位置就退出了
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 阻塞等待子線程退出
- void* ptr = NULL;
- // ptr是一個傳出參數(shù), 在函數(shù)內(nèi)部讓這個指針指向一塊有效內(nèi)存
- // 這個內(nèi)存地址就是pthread_exit() 參數(shù)指向的內(nèi)存
- pthread_join(tid, &ptr);
- // 打印信息
- struct Persion* pp = (struct Persion*)ptr;
- printf("子線程返回數(shù)據(jù): name: %s, age: %d, id: %d\n", pp->name, pp->age, pp->id);
- printf("子線程資源被成功回收...\n");
- return 0;
- }
編譯并執(zhí)行測試程序:
- # 編譯代碼
- $ gcc pthread_join.c -lpthread
- # 執(zhí)行程序
- $ ./a.out
- 子線程創(chuàng)建成功, 線程ID: 140652794640128
- 我是主線程, 線程ID: 140652803008256
- i = 0
- i = 1
- i = 2
- 我是子線程, 線程ID: 140652794640128
- child == i: = 0
- child == i: = 1
- child == i: = 2
- child == i: = 3
- child == i: = 4
- child == i: = 5
- child == i: = 6
- 子線程返回數(shù)據(jù): name: , age: 0, id: 0
- 子線程資源被成功回收...
通過打印的日志可以發(fā)現(xiàn),在主線程中沒有沒有得到子線程返回的數(shù)據(jù)信息,具體原因是這樣的:
如果多個線程共用同一個虛擬地址空間,每個線程在棧區(qū)都有一塊屬于自己的內(nèi)存,相當(dāng)于棧區(qū)被這幾個線程平分了,當(dāng)線程退出,線程在棧區(qū)的內(nèi)存也就被回收了,因此隨著子線程的退出,寫入到棧區(qū)的數(shù)據(jù)也就被釋放了。
4.2.2 使用全局變量
位于同一虛擬地址空間中的線程,雖然不能共享棧區(qū)數(shù)據(jù),但是可以共享全局?jǐn)?shù)據(jù)區(qū)和堆區(qū)數(shù)據(jù),因此在子線程退出的時候可以將傳出數(shù)據(jù)存儲到全局變量、靜態(tài)變量或者堆內(nèi)存中。在下面的例子中將數(shù)據(jù)存儲到了全局變量中:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 定義結(jié)構(gòu)
- struct Persion
- {
- int id;
- char name[36];
- int age;
- };
- struct Persion p; // 定義全局變量
- // 子線程的處理代碼
- void* working(void* arg)
- {
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf("child == i: = %d\n", i);
- if(i == 6)
- {
- // 使用全局變量
- p.age =12;
- strcpy(p.name, "tom");
- p.id = 100;
- // 該函數(shù)的參數(shù)將這個地址傳遞給了主線程的pthread_join()
- pthread_exit(&p);
- }
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 阻塞等待子線程退出
- void* ptr = NULL;
- // ptr是一個傳出參數(shù), 在函數(shù)內(nèi)部讓這個指針指向一塊有效內(nèi)存
- // 這個內(nèi)存地址就是pthread_exit() 參數(shù)指向的內(nèi)存
- pthread_join(tid, &ptr);
- // 打印信息
- struct Persion* pp = (struct Persion*)ptr;
- printf("name: %s, age: %d, id: %d\n", pp->name, pp->age, pp->id);
- printf("子線程資源被成功回收...\n");
- return 0;
- }
4.2.3 使用主線程棧
雖然每個線程都有屬于自己的棧區(qū)空間,但是位于同一個地址空間的多個線程是可以相互訪問對方的??臻g上的數(shù)據(jù)的。由于很多情況下還需要在主線程中回收子線程資源,所以主線程一般都是最后退出,基于這個原因在下面的程序中將子線程返回的數(shù)據(jù)保存到了主線程的棧區(qū)內(nèi)存中:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 定義結(jié)構(gòu)
- struct Persion
- {
- int id;
- char name[36];
- int age;
- };
- // 子線程的處理代碼
- void* working(void* arg)
- {
- struct Persion* p = (struct Persion*)arg;
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf("child == i: = %d\n", i);
- if(i == 6)
- {
- // 使用主線程的棧內(nèi)存
- p->age =12;
- strcpy(p->name, "tom");
- p->id = 100;
- // 該函數(shù)的參數(shù)將這個地址傳遞給了主線程的pthread_join()
- pthread_exit(p);
- }
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- struct Persion p;
- // 主線程的棧內(nèi)存?zhèn)鬟f給子線程
- pthread_create(&tid, NULL, working, &p);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 阻塞等待子線程退出
- void* ptr = NULL;
- // ptr是一個傳出參數(shù), 在函數(shù)內(nèi)部讓這個指針指向一塊有效內(nèi)存
- // 這個內(nèi)存地址就是pthread_exit() 參數(shù)指向的內(nèi)存
- pthread_join(tid, &ptr);
- // 打印信息
- printf("name: %s, age: %d, id: %d\n", p.name, p.age, p.id);
- printf("子線程資源被成功回收...\n");
- return 0;
- }
在上面的程序中,調(diào)用 pthread_create() 創(chuàng)建子線程,并將主線程中棧空間變量 p 的地址傳遞到了子線程中,在子線程中將要傳遞出的數(shù)據(jù)寫入到了這塊內(nèi)存中。也就是說在程序的 main() 函數(shù)中,通過指針變量 ptr 或者通過結(jié)構(gòu)體變量 p 都可以讀出子線程傳出的數(shù)據(jù)。
五、線程分離
在某些情況下,程序中的主線程有屬于自己的業(yè)務(wù)處理流程,如果讓主線程負(fù)責(zé)子線程的資源回收,調(diào)用 pthread_join() 只要子線程不退出主線程就會一直被阻塞,主要線程的任務(wù)也就不能被執(zhí)行了。
在線程庫函數(shù)中為我們提供了線程分離函數(shù) pthread_detach(),調(diào)用這個函數(shù)之后指定的子線程就可以和主線程分離,當(dāng)子線程退出的時候,其占用的內(nèi)核資源就被系統(tǒng)的其他進(jìn)程接管并回收了。線程分離之后在主線程中使用 pthread_join() 就回收不到子線程資源了。
- #include <pthread.h>
- // 參數(shù)就子線程的線程ID, 主線程就可以和這個子線程分離了
- int pthread_detach(pthread_t thread);
下面的代碼中,在主線程中創(chuàng)建子線程,并調(diào)用線程分離函數(shù),實現(xiàn)了主線程和子線程的分離:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 子線程的處理代碼
- void* working(void* arg)
- {
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf("child == i: = %d\n", i);
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 設(shè)置子線程和主線程分離
- pthread_detach(tid);
- // 讓主線程自己退出即可
- pthread_exit(NULL);
- return 0;
- }
六、其他線程函數(shù)
6.1 線程取消
線程取消的意思就是在某些特定情況下在一個線程中殺死另一個線程。使用這個函數(shù)殺死一個線程需要分兩步:
- 在線程 A 中調(diào)用線程取消函數(shù) pthread_cancel,指定殺死線程 B,這時候線程 B 是死不了的
- 在線程 B 中進(jìn)程一次系統(tǒng)調(diào)用(從用戶區(qū)切換到內(nèi)核區(qū)),否則線程 B 可以一直運(yùn)行。
這其實和七步斷腸散、含笑半步癲的功效是一樣的,吃了毒藥不動或者不笑也沒啥事兒
- #include <pthread.h>
- // 參數(shù)是子線程的線程ID
- int pthread_cancel(pthread_t thread);
- 參數(shù):要殺死的線程的線程 ID
- 返回值:函數(shù)調(diào)用成功返回 0,調(diào)用失敗返回非 0 錯誤號。
在下面的示例代碼中,主線程調(diào)用線程取消函數(shù),只要在子線程中進(jìn)行了系統(tǒng)調(diào)用,當(dāng)子線程執(zhí)行到這個位置就掛掉了。
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- // 子線程的處理代碼
- void* working(void* arg)
- {
- int j=0;
- for(int i=0; i<9; ++i)
- {
- j++;
- }
- // 這個函數(shù)會調(diào)用系統(tǒng)函數(shù), 因此這是個間接的系統(tǒng)調(diào)用
- printf("我是子線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<9; ++i)
- {
- printf(" child i: %d\n", i);
- }
- return NULL;
- }
- int main()
- {
- // 1. 創(chuàng)建一個子線程
- pthread_t tid;
- pthread_create(&tid, NULL, working, NULL);
- printf("子線程創(chuàng)建成功, 線程ID: %ld\n", tid);
- // 2. 子線程不會執(zhí)行下邊的代碼, 主線程執(zhí)行
- printf("我是主線程, 線程ID: %ld\n", pthread_self());
- for(int i=0; i<3; ++i)
- {
- printf("i = %d\n", i);
- }
- // 殺死子線程, 如果子線程中做系統(tǒng)調(diào)用, 子線程就結(jié)束了
- pthread_cancel(tid);
- // 讓主線程自己退出即可
- pthread_exit(NULL);
- return 0;
- }
關(guān)于系統(tǒng)調(diào)用有兩種方式:
- 直接調(diào)用 Linux 系統(tǒng)函數(shù)
- 調(diào)用標(biāo)準(zhǔn) C 庫函數(shù),為了實現(xiàn)某些功能,在 Linux 平臺下標(biāo)準(zhǔn) C 庫函數(shù)會調(diào)用相關(guān)的系統(tǒng)函數(shù)
6.2 線程 ID 比較
在 Linux 中線程 ID 本質(zhì)就是一個無符號長整形,因此可以直接使用比較操作符比較兩個線程的 ID,但是線程庫是可以跨平臺使用的,在某些平臺上 pthread_t 可能不是一個單純的整形,這中情況下比較兩個線程的 ID 必須要使用比較函數(shù),函數(shù)原型如下:
- #include <pthread.h>
- int pthread_equal(pthread_t t1, pthread_t t2);
- 參數(shù):t1 和 t2 是要比較的線程的線程 ID
- 返回值:如果兩個線程 ID 相等返回非 0 值,如果不相等返回 0