Linux多線程函數(shù)用得比較多的是下面的3個(gè)
pthread_create(),pthread_exit(),pthread_join();它們都是在頭文件之中。編譯時(shí)需要加靜態(tài)庫(kù)-lpthread
下面是函數(shù)的說明:
pthread_create是UNIX環(huán)境創(chuàng)建線程函數(shù)
int pthread_create(
pthread_t *restrict tidp,
const pthread_attr_t *restrict_attr,
void*(*start_rtn)(void*),
void *restrict arg);
返回值
若成功則返回0,否則返回出錯(cuò)編號(hào)
返回成功時(shí),由tidp指向的內(nèi)存單元被設(shè)置為新創(chuàng)建線程的線程ID。attr參數(shù)用于制定各種不同的線程屬性。新創(chuàng)建的線程從start_rtn函數(shù)的地址開始運(yùn)行,該函數(shù)只有一個(gè)指針參數(shù)arg,如果需要向start_rtn函數(shù)傳遞的參數(shù)不止一個(gè),那么需要把這些參數(shù)放到一個(gè)結(jié)構(gòu)中,然后把這個(gè)結(jié)構(gòu)的地址作為arg的參數(shù)傳入。
linux下用C開發(fā)多線程程序,Linux系統(tǒng)下的多線程遵循POSIX線程接口,稱為pthread。
由 restrict 修飾的指針是初對(duì)指針?biāo)赶虻膶?duì)象進(jìn)行存取的方法,僅當(dāng)?shù)诙€(gè)指針基于個(gè)時(shí),才能對(duì)對(duì)象進(jìn)行存取。對(duì)對(duì)象的存取都限定于基于由 restrict 修飾的指針表達(dá)式中。 由 restrict 修飾的指針主要用于函數(shù)形參,或指向由 malloc() 分配的內(nèi)存空間。restrict 數(shù)據(jù)類型不改變程序的語(yǔ)義。 編譯器能通過作出 restrict 修飾的指針是存取對(duì)象的方法的假設(shè),更好地優(yōu)化某些類型的例程。
參數(shù)
個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針。
第二個(gè)參數(shù)用來設(shè)置線程屬性。
第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址。
一個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。
另外,在編譯時(shí)注意加上-lpthread參數(shù),以調(diào)用靜態(tài)鏈接庫(kù)。因?yàn)閜thread并非Linux系統(tǒng)的默認(rèn)庫(kù)
pthread_exit(void* retval);
線程通過調(diào)用pthread_exit函數(shù)終止自身執(zhí)行,就如同進(jìn)程在結(jié)束時(shí)調(diào)用exit函數(shù)一樣。這個(gè)函數(shù)的作用是,終止調(diào)用它的線程并返回一個(gè)指向某個(gè)對(duì)象的指針。該指針可以通過pthread_join(pthread_t tpid, void **value_ptr)中的第二個(gè)參數(shù)value_ptr獲取到。
函數(shù)pthread_join用來等待一個(gè)線程的結(jié)束。函數(shù)原型為:
extern int pthread_join __P (pthread_t __th, void **__thread_return);
個(gè)參數(shù)為被等待的線程標(biāo)識(shí)符,第二個(gè)參數(shù)為一個(gè)用戶定義的指針,它可以用來存儲(chǔ)被等待線程退出時(shí)的返回值。這個(gè)函數(shù)是一個(gè)線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源被收回。如果執(zhí)行成功,將返回0,如果失敗則返回一個(gè)錯(cuò)誤號(hào)。
所有線程都有一個(gè)線程號(hào),也就是Thread ID。其類型為pthread_t。通過調(diào)用pthread_self()函數(shù)可以獲得自身的線程號(hào)。
下面是一個(gè)簡(jiǎn)單的例子,子線程thread_fun會(huì)打出5次“this is thread_fun print!”然后調(diào)用pthread_exit退出,并返回一個(gè)指向字符串“this is thread return value!”的指針。在主函數(shù)里面調(diào)用pthread_join等待thread_fun線程結(jié)束,然后讀取子線程的返回值到value中,再打印出來。
輸出結(jié)果是:
pthread_create ok!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
pthread exit value: this is thread return value!
01.#include
02.#include
03.#include
04.#include
05.#include
06.#include
07.//////////////////////////////////////////////////////
08.void *thread_fun(void *arg) {
09. int i=0;
10. char *value_ptr = "this is thread return value!n";
11. for (i=0; i<5; i++) {
12. printf("this is thread_fun print!n");
13. sleep(1);
14. }
15. pthread_exit((void*)value_ptr);
16.}
17.//////////////////////////////////////////////////////
18.int main(int argc, char **argv) {
19. pthread_t pid;
20. int ret;
21. void* value;
22.
23. ret = pthread_create(&pid, NULL, thread_fun, NULL);
24. if (ret) {
25. printf("pthread_create failed!nerrno:%dn", errno);
26. return -1;
27. }
28. printf("pthread_create ok!n");
29.
30. pthread_join(pid, &value);
31. printf("pthread exit value: %sn", value);
32. return 0;
33.}
34.
35.