linux多執行緒學習 二 執行緒的建立和退出

2021-05-10 13:55:01 字數 1870 閱讀 4412

1、函式語法簡述。

pthread_create

標頭檔案:       pthread.h

函式原型:    int pthread_create (pthread_t* thread, pthread_attr_t* attr,

void* (start_routine)(void*), void* arg);

函式傳入值: thread: 執行緒識別符號

attr: 執行緒屬性設定

start_routine:執行緒函式入口

arg:傳遞給執行緒函式的引數

返回值:       0: 成功

-1: 失敗

pthread_exit

標頭檔案:      pthread.h

函式原型:   void pthread_exit (void*  retval);

函式傳入值:retval:pthread_exit()呼叫者執行緒的返回值,可又其他函式如pthread_join來檢索獲取。

phread_join

標頭檔案:      pthread.h

函式原型:   int pthread_join (pthread_t* thread, void** thread_return);

函式傳入值:thread:等待執行緒的識別符號。

thread_return:使用者定義的指標,用來儲存被等待執行緒的返回值(不為null值);

函式返回值:成功: 0

失敗:-1

2、函式舉例實現。

view plaincopy to clipboardprint?

#include

#include

#include

#include

static void pthread_func_1 (void);  

static void pthread_func_2 (void);  

int main (int argc, char** argv)  

ret = pthread_create (&pt_2, null, pthread_func_2, null);  

if (ret != 0)  

pthread_join (pt_1, null);  

pthread_join (pt_2, null);  

return 0;  

}  

static void pthread_func_1 (void)  

sleep (1);  

}  

}  

static void pthread_func_2 (void)  

pthread_exit (0);  

}  #include

#include

#include

#include

static void pthread_func_1 (void);

static void pthread_func_2 (void);

int main (int argc, char** argv)

ret = pthread_create (&pt_2, null, pthread_func_2, null);

if (ret != 0)

pthread_join (pt_1, null);

pthread_join (pt_2, null);

return 0;

}static void pthread_func_1 (void)

sleep (1);}}

static void pthread_func_2 (void)

pthread_exit (0);

}

Linux多執行緒程式設計(二) 執行緒屬性

pthread介面允許我們通過設定每個物件關聯的不同屬性來細調執行緒的行為。include int pthread attr init pthread attr t attr int pthread attr destroy pthread attr t attr 兩個函式的返回值 若成功,返回0 ...

linux多執行緒學習 二 執行緒的建立和退出

1 函式語法簡述。pthread create 標頭檔案 pthread.h 函式原型 int pthread create pthread t thread,pthread attr t attr,void start routine void void arg 函式傳入值 thread 執行緒識...

linux多執行緒學習 二 執行緒的建立和退出

1 函式語法簡述。pthread create 標頭檔案 pthread.h 函式原型 int pthread create pthread t thread,pthread attr t attr,void start routine void void arg 函式傳入值 thread 執行緒識...