關於C語言多執行緒pthread庫的相關函式說明

2022-10-04 01:09:14 字數 3372 閱讀 1469

執行緒相關操作說明

一 pthread_t

pthread_t在標頭檔案/usr/include/bits/pthreadtypes.h中定義:

typedef unsigned long int pthread_t;

它是乙個執行緒的識別符號。

二 pthread_create

函式pthread_create用來建立乙個執行緒,它的原型為:

extern int pthread_create __p ((pthread_t *__thread, __const pthread_attr_t *__attr,

void *(*__start_routine) (void *), void *__arg));

第乙個引數為指向執行緒識別符號的指標,第二個引數用來設定執行緒屬性,第三個引數是執行緒執行函式的起始位址,最後乙個引數是執行函式的引數。這裡,我們的函式thread不需要引數,所以最後乙個引數設為空指標。第二個引數我們也設為空指標,這樣將生成預設屬性的執行緒。對執行緒屬性的設定和修改我們將在下一節闡述。當建立執行緒成功時,函式返回0,若不為0則說明建立執行緒失敗,常見的錯誤返回**為eagain和einval.前者表示系統限制建立新的執行緒,例如執行緒數目過多了;後者表示第二個引數代表的執行緒屬性值非法。建立執行緒成功後,新建立的執行緒則執行引數三和引數四確定的函式,原來的執行緒則繼續執行下一行**。

三 pthread_join pthread_exit

函式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.下面,我們來了解執行緒的一些常用屬性以及如何設定這些屬性。

互斥鎖相關

互斥鎖用來保證一段時間內只有乙個執行緒在執行一段**。

一 pthread_mutex_init

函式pthread_mutex_init用來生成乙個互斥鎖。null引數表明使用預設屬性。如果需要宣告特定屬性的互斥鎖,須呼叫函式 pthread_mutexattr_init.函式pthread_mutexattr_setpshared和函式 pthread_mutexattr_settype用來設定互斥鎖屬性。前乙個函式設定屬性pshared,它有兩個取值, pthread_process_private和pthread_process_shared.前者用來不同程序中的執行緒同步,後者用於同步本程序的不同執行緒。在上面的例子中,我們使用的是預設屬性pthread_process_ private.後者用來設定互斥鎖型別,可選的型別有pthread_mutex_normal、pthread_mutex_errorcheck、 pthread_mutex_recursive和pthread _mutex_default.它們分別定義了不同的上所、解鎖機制,一般情況下,選用最後乙個預設屬性。

二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np

pthread_mutex_lock宣告開始用互斥鎖上鎖,此後的**直至呼叫pthread_mutex_unlock為止,均被上鎖,即同一時間只能被乙個執行緒呼叫執行。當乙個執行緒執行到pthread_mutex_lock處時,如果該鎖此時被另乙個執行緒使用,那此執行緒被阻塞,即程式將等待到另乙個執行緒釋放此互斥鎖。

下面先來乙個例項。我們通過建立兩個執行緒來實現對乙個數的遞加。

#include

#include

#include

#include

#define max 10

pthread_t thread[2];

pthread_mutex_t mut;

int number=0, i;

void *thread1()

printf("thread1 :主函式在等我完成任務嗎?\n");

pthread_exit(null);

} void *thread2()

printf("thread2 :主函式在等我完成任務嗎?\n");

pthread_exit(null);

} voiwww.cppcns.comd thread_create(void)

void thread_wait(void)

if(thread[1] !=0)

} int main()

下面我們先來編譯、執行一下

引文:falcon@falcon:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c

falcon@falcon:~/program/c/code/ftp$ ./thread_example

我是主函式哦,我正在建立執行緒,呵呵

執行緒1被建立

執行緒程式設計客棧2被建立

我是主函式哦,我正在等待執行緒完成任務阿,呵呵

thread1 : i'm thread 1

thread1 : number = 0

thread2 : i'm thread 2

thread2 : number = 1

thread1 : number = 2

thread2 : number = 3

thread1 : number = 4

thread2 : number = 5

thread1 : number = 6

thread1 : number = 7

thread2 : number = 8

thread1 : number = 9

thread2 : number = 10

thread1 :主函式在等我完成任務嗎?

執行緒1已經結束

thread2 :主函式在等我完成任務嗎?

執行緒2已經結束

pthread 多執行緒

多執行緒程式指的是在同乙個程式中多個執行流併發執行,它們共享程序的同乙個位址空間,分別完成相應的任務,並通過共享位址空間等方式完成執行緒間通訊,cpu按照時間片輪轉等方式對執行緒進行切換和排程。通常而言,執行緒共享的程序資源包括 linux中線程的建立依賴於lpthread.so 庫,建立乙個thr...

pthread多執行緒計時函式 C

在使用多執行緒開發過程中,發現無論怎麼修改,多執行緒的效率提公升都不是很明顯,經過多次排查發現是計時函式的應用出現了問題。此前使用clock 計時,最終改為timeval函式,得到正確的執行時間。記錄下不同的計時函式的特點。clock 函式為最常用的計時函式,返回end與begin之間的cpu時鐘計...

pthread建立多執行緒

include include include include include include include include include include include tinyxml tinyxml.h include include include include define macxm...