複製鏈接
請複製以下鏈接發送給好友

pthread_create

鎖定
pthread_create是類Unix操作系統(UnixLinuxMac OS X等)的創建線程的函數。它的功能是創建線程(實際上就是確定調用該線程函數的入口點),在線程創建以後,就開始運行相關的線程函數。
pthread_create的返回值:若成功,返回0;若出錯,返回出錯編號。
中文名
線程的函數
外文名
pthread_create
類    型
操作系統
編譯鏈接參數
pthread

pthread_create函數簡介

pthread_create頭文件

#include<pthread.h>

pthread_create函數聲明

int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,
void *(*start_rtn)(void*),void *arg);

pthread_create編譯鏈接參數

-lpthread

pthread_create返回值

若線程創建成功,則返回0。若線程創建失敗,則返回出錯編號,並且*thread中的內容是未定義的。
返回成功時,由tidp指向的內存單元被設置為新創建線程的線程ID。attr參數用於指定各種不同的線程屬性。新創建的線程從start_rtn函數的地址開始運行,該函數只有一個萬能指針參數arg,如果需要向start_rtn函數傳遞的參數不止一個,那麼需要把這些參數放到一個結構中,然後把這個結構的地址作為arg的參數傳入。
linux下用C語言開發多線程程序,Linux系統下的多線程遵循POSIX線程接口,稱為pthread。

pthread_create參數

第一個參數為指向線程標識符的指針。
第二個參數用來設置線程屬性。
第三個參數是線程運行函數的起始地址。
最後一個參數是運行函數的參數。

pthread_create注意事項

因為pthread並非Linux系統的默認庫,而是POSIX線程庫。在Linux中將其作為一個庫來使用,因此加上 -lpthread(或-pthread)以顯式鏈接該庫。函數在執行錯誤時的錯誤信息將作為返回值返回,並不修改系統全局變量errno,當然也無法使用perror打印錯誤信息。

pthread_create示例

pthread_create輸出線程標識符

#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)

pthread_create簡單的線程程序

#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)(*((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);
        //此處t變量的用法是方便大家調測代碼的寫法,實際使用會有問題,因為這個t是局部變量,
        函數執行完後馬上釋放,大家傳遞參數時需要使用全局變量或malloc出來的變量。
        		// 可寫為 rc = pthread_create(&thread[t], NULL, PrintHello, (void*) t)
                // 在線程中,為thread_arg = (int)arg;
        rc = pthread_create(&thread[t], NULL, PrintHello, &t);
        if (rc)
        {
            printf("ERROR; return code is %d\n", rc);
            return EXIT_FAILURE;
        }
    }
    sleep(5);
    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 8
Hello from thread 8
Hello from thread 8
Hello from thread 8
Hello from thread 8
Hello from thread 8
Hello from thread 8
Hello from thread 8