pthreads是一種POSIX線程庫標準,可以在多個系統中使用。在C和C++中使用pthread庫以及pthread_create函數來創建和控制線程。
一、pthread_create()介紹
pthread_create()函數用於創建一個線程。這個函數被大多數POSIX線程庫實現支持。這個函數的原型如下:
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
pthread_create()的參數:
- thread: 指向pthread_t類型的指針,返回線程的真實標識符。
- attr(Optional): 線程屬性。
- start_routine: 指向線程執行的函數。線程函數的類型為 void *(*start_routine) (void *arguments),其中arguments是傳遞給該線程函數的參數。
- arg: 傳遞給線程函數的參數。
二、創建線程的方法
1. 傳遞函數指針
我們可以像下面這樣定義一個線程函數:
#include <stdio.h>
#include <pthread.h>
void* threadFunc1() {
printf("Hello, this is threadFunc1\n");
pthread_exit(NULL);
}
int main() {
pthread_t thread1;
printf("Before creating thread\n");
pthread_create(&thread1, NULL, threadFunc1, NULL);
printf("After creating thread\n");
pthread_exit(NULL);
}
運行結果:
Before creating thread
After creating thread
Hello, this is threadFunc1
2. 傳遞一個對象提供線程函數訪問
如下示例代碼,我們可以定義一個結構,並將結構作為參數傳遞給線程函數。
#include <stdio.h>
#include <pthread.h>
struct thread_data {
int thread_id;
char *message;
};
void* threadFunc2(void *args) {
struct thread_data *data = (struct thread_data *) args;
printf("Thread ID: %d\n", data -> thread_id);
printf("Message: %s\n", data -> message);
pthread_exit(NULL);
}
int main() {
pthread_t thread2;
struct thread_data data;
data.thread_id = 123;
data.message = "Hello, this is threadFunc2";
printf("Before creating thread\n");
pthread_create(&thread2, NULL, threadFunc2, (void *) &data);
printf("After creating thread\n");
pthread_exit(NULL);
}
運行結果:
Before creating thread
After creating thread
Thread ID: 123
Message: Hello, this is threadFunc2
三、線程屬性
可以使用pthread_attr_init()函數來初始化一個線程屬性對象,然後使用這個線程屬性對象來設置線程的屬性。以下示例代碼展示如何使用pthread_attr_setdetachstate()設置線程的分離屬性。
#include <stdio.h>
#include <pthread.h>
void* threadFunc3() {
printf("Hello, this is threadFunc3\n");
pthread_exit(NULL);
}
int main() {
pthread_t thread3;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
printf("Before creating thread\n");
pthread_create(&thread3, &attr, threadFunc3, NULL);
printf("After creating thread\n");
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
運行結果:
Before creating thread
After creating thread
Hello, this is threadFunc3
四、線程ID和join()
每個線程都有一個唯一標識符的線程ID,可以使用pthread_self()函數獲取當前線程的ID。
可以使用pthread_join()來等待線程終止。和wait()函數類似,pthread_join()會阻塞調用者直到指定的線程終止。以下示例代碼在創建線程後等待線程終止。
#include <stdio.h>
#include <pthread.h>
void* threadFunc4() {
printf("Hello, this is threadFunc4\n");
pthread_exit(NULL);
}
int main() {
pthread_t thread4;
printf("Before creating thread\n");
pthread_create(&thread4, NULL, threadFunc4, NULL);
printf("After creating thread\n");
pthread_join(thread4, NULL);
pthread_exit(NULL);
}
運行結果:
Before creating thread
After creating thread
Hello, this is threadFunc4
五、總結
pthreads是一種POSIX線程庫標準,可以在多個系統中使用。pthread_create()函數用於創建一個線程。可以傳遞函數指針或一個對象作為參數傳遞,也可以設置線程屬性和獲取線程ID。我們可以使用pthread_join()來等待線程終止。對於多線程編程,合理的控制線程並發和同步是很重要的。
原創文章,作者:KFXPO,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/362008.html
微信掃一掃
支付寶掃一掃