天天看點

pthread_create

轉自:http://baike.baidu.com/view/1797052.htm?fr=aladdin

pthread_create是類Unix作業系統(Unix、Linux、Mac OS X等)的建立 線程的函數。

目錄

1函數簡介
▪  頭檔案
▪  函數聲明
▪  編譯連結參數
▪  傳回值
▪  參數
▪  注意事項
2示例
▪  輸出線程辨別符
▪  簡單的線程程式

1函數簡介編輯

頭檔案

1

#include<pthread.h>

[1]  

函數聲明

1 2

intpthread_create(pthread_t*tidp,constpthread_attr_t*attr,

(

void

*)(*start_rtn)(

void

*),

void

*arg);

[1]  

編譯連結參數

-pthread [1]  

傳回值

若線程建立成功,則傳回0。若線程建立失敗,則傳回出錯編号,并且*thread中的内容是未定義的。 [1]   傳回成功時,由tidp指向的記憶體單元被設定為新建立線程的線程ID。attr參數用于指定各種不同的線程屬性。新建立的線程從start_rtn函數的位址開始運作,該函數隻有一個萬能 指針參數arg,如果需要向start_rtn函數傳遞的參數不止一個,那麼需要把這些參數放到一個結構中,然後把這個結構的位址作為arg的參數傳入。 linux下用C語言開發多線程程式,Linux系統下的多線程遵循POSIX線程接口,稱為pthread。 由 restrict 修飾的 指針是最初唯一對指針所指向的對象進行存取的方法,僅當第二個指針基于第一個時,才能對對象進行存取。對對象的存取都限定于基于由 restrict 修飾的 指針表達式中。 由 restrict 修飾的 指針主要用于函數 形參,或指向由 malloc() 配置設定的記憶體空間。restrict  資料類型不改變程式的語義。  編譯器能通過作出 restrict 修飾的 指針是存取對象的唯一方法的假設,更好地優化某些類型的例程。

參數

第一個參數為指向線程 辨別符的 指針。 第二個參數用來設定線程屬性。 第三個參數是線程運作函數的起始位址。 最後一個參數是運作函數的參數。

注意事項

因為pthread并非Linux系統的預設庫,而是POSIX線程庫。在Linux中将其作為一個庫來使用,是以加上 -lpthread(或-pthread)以顯式連結該庫。函數在執行錯誤時的錯誤資訊将作為傳回值傳回,并不修改系統全局變量errno,當然也無法使用perror()列印錯誤資訊。

2示例編輯

輸出線程辨別符

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

#include <string.h>

void

printids(

const

char

*s)

{

pid_t pid;

pthread_t tid;

pid = getpid();

tid = pthread_self();

printf

(

"%s pid %u tid %u (0x%x)\n"

, s, (unsigned

int

) pid,

(unsigned

int

) tid, (unsigned

int

) tid);

}

void

*thr_fn(

void

*arg)

{

printids(

"new thread: "

);

return

NULL;

}

int

main(

void

)

{

int

err;

pthread_t ntid;

err = pthread_create(&ntid, NULL, thr_fn, NULL);

if

(err != 0)

printf

(

"can't create thread: %s\n"

,

strerror

(err));

printids(

"main thread:"

);

pthread_join(ntid,NULL);

return

EXIT_SUCCESS;

}

$ gcc main.c -o main -std=c99 -pthread $ ./main main thread: pid 13073 tid 3077572816 (0xb77008d0) new thread: pid 13073 tid 3077569392 (0xb76ffb70)

簡單的線程程式

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

#define    NUM_THREADS     8

void

*PrintHello(

void

*args)

{

int

thread_arg;

sleep(1);

thread_arg = (

int

)args;

printf

(

"Hello from thread %d\n"

, thread_arg);

return

NULL;

}

int

main(

void

)

{

int

rc,t;

pthread_t

thread

[NUM_THREADS];

for

( t = 0; t < NUM_THREADS; t++)

{

printf

(

"Creating thread %d\n"

, t);

rc = pthread_create(&

thread

[t], NULL, PrintHello, (

void

*)t);

if

(rc)

{

printf

(

"ERROR; return code is %d\n"

, rc);

return

EXIT_FAILURE;

}

}

for

( t = 0; t < NUM_THREADS; t++)

pthread_join(

thread

[t], NULL);

return

EXIT_SUCCESS;

}

$ gcc thread_test.c -o thread_test -std=c99 -pthread $ ./thread_test Creating thread 0 Creating thread 1 Creating thread 2 Creating thread 3 Creating thread 4 Creating thread 5 Creating thread 6 Creating thread 7 Hello from thread 1 Hello from thread 2 Hello from thread 3 Hello from thread 4 Hello from thread 5 Hello from thread 6 Hello from thread 7 Hello from thread 0

繼續閱讀