深入了解pthread_create()

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-tw/n/362008.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
KFXPO的頭像KFXPO
上一篇 2025-02-25 18:17
下一篇 2025-02-25 18:17

相關推薦

  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25
  • 深入了解LaTeX的腳註(latexfootnote)

    一、基本介紹 LaTeX作為一種排版軟體,具有各種各樣的功能,其中腳註(footnote)是一個十分重要的功能之一。在LaTeX中,腳註是用命令latexfootnote來實現的。…

    編程 2025-04-25
  • 深入剖析MapStruct未生成實現類問題

    一、MapStruct簡介 MapStruct是一個Java bean映射器,它通過註解和代碼生成來在Java bean之間轉換成本類代碼,實現類型安全,簡單而不失靈活。 作為一個…

    編程 2025-04-25
  • 深入理解Python字元串r

    一、r字元串的基本概念 r字元串(raw字元串)是指在Python中,以字母r為前綴的字元串。r字元串中的反斜杠(\)不會被轉義,而是被當作普通字元處理,這使得r字元串可以非常方便…

    編程 2025-04-25
  • 深入探討馮諾依曼原理

    一、原理概述 馮諾依曼原理,又稱「存儲程序控制原理」,是指計算機的程序和數據都存儲在同一個存儲器中,並且通過一個統一的匯流排來傳輸數據。這個原理的提出,是計算機科學發展中的重大進展,…

    編程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一個程序就是一個模塊,而一個模塊可以引入另一個模塊,這樣就形成了包。包就是有多個模塊組成的一個大模塊,也可以看做是一個文件夾。包可以有效地組織代碼和數據…

    編程 2025-04-25

發表回復

登錄後才能評論