C++ 教程
多線程是多任務(wù)處理的一種特殊形式,多任務(wù)處理允許讓電腦同時運行兩個或兩個以上的程序。一般情況下,兩種類型的多任務(wù)處理:基于進程和基于線程。
多線程程序包含可以同時運行的兩個或多個部分。這樣的程序中的每個部分稱為一個線程,每個線程定義了一個單獨的執(zhí)行路徑。
本教程假設(shè)您使用的是 Linux 操作系統(tǒng),我們要使用 POSIX 編寫多線程 C++ 程序。POSIX Threads 或 Pthreads 提供的 API 可在多種類 Unix POSIX 系統(tǒng)上可用,比如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。
下面的程序,我們可以用它來創(chuàng)建一個 POSIX 線程:
#include <pthread.h> pthread_create (thread, attr, start_routine, arg)
在這里,pthread_create 創(chuàng)建一個新的線程,并讓它可執(zhí)行。下面是關(guān)于參數(shù)的說明:
參數(shù) | 描述 |
---|---|
thread | 指向線程標(biāo)識符指針。 |
attr | 一個不透明的屬性對象,可以被用來設(shè)置線程屬性。您可以指定線程屬性對象,也可以使用默認值 NULL。 |
start_routine | 線程運行函數(shù)起始地址,一旦線程被創(chuàng)建就會執(zhí)行。 |
arg | 運行函數(shù)的參數(shù)。它必須通過把引用作為指針強制轉(zhuǎn)換為 void 類型進行傳遞。如果沒有傳遞參數(shù),則使用 NULL。 |
創(chuàng)建線程成功時,函數(shù)返回 0,若返回值不為 0 則說明創(chuàng)建線程失敗。
使用下面的程序,我們可以用它來終止一個 POSIX 線程:
#include <pthread.h> pthread_exit (status)
在這里,pthread_exit 用于顯式地退出一個線程。通常情況下,pthread_exit() 函數(shù)是在線程完成工作后無需繼續(xù)存在時被調(diào)用。
如果 main() 是在它所創(chuàng)建的線程之前結(jié)束,并通過 pthread_exit() 退出,那么其他線程將繼續(xù)執(zhí)行。否則,它們將在 main() 結(jié)束時自動被終止。
以下簡單的實例代碼使用 pthread_create() 函數(shù)創(chuàng)建了 5 個線程,每個線程輸出"Hello Runoob!":
使用 -lpthread 庫編譯下面的程序:
$ g++ test.cpp -lpthread -o test.o
現(xiàn)在,執(zhí)行程序,將產(chǎn)生下列結(jié)果:
$ ./test.o Hello Runoob! Hello Runoob! Hello Runoob! Hello Runoob! Hello Runoob!
以下簡單的實例代碼使用 pthread_create() 函數(shù)創(chuàng)建了 5 個線程,并接收傳入的參數(shù)。每個線程打印一個 "Hello Runoob!" 消息,并輸出接收的參數(shù),然后調(diào)用 pthread_exit() 終止線程。
現(xiàn)在編譯并執(zhí)行程序,將產(chǎn)生下列結(jié)果:
$ g++ test.cpp -lpthread -o test.o $ ./test.o main() : 創(chuàng)建線程, 0 main() : 創(chuàng)建線程, 1 Hello Runoob! 線程 ID, 0 main() : 創(chuàng)建線程, Hello Runoob! 線程 ID, 21 main() : 創(chuàng)建線程, 3 Hello Runoob! 線程 ID, 2 main() : 創(chuàng)建線程, 4 Hello Runoob! 線程 ID, 3 Hello Runoob! 線程 ID, 4
這個實例演示了如何通過結(jié)構(gòu)傳遞多個參數(shù)。您可以在線程回調(diào)中傳遞任意的數(shù)據(jù)類型,因為它指向 void,如下面的實例所示:
當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
$ 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
我們可以使用以下兩個函數(shù)來連接或分離線程:
pthread_join (threadid, status) pthread_detach (threadid)
pthread_join() 子程序阻礙調(diào)用程序,直到指定的 threadid 線程終止為止。當(dāng)創(chuàng)建一個線程時,它的某個屬性會定義它是否是可連接的(joinable)或可分離的(detached)。只有創(chuàng)建時定義為可連接的線程才可以被連接。如果線程創(chuàng)建時被定義為可分離的,則它永遠也不能被連接。
這個實例演示了如何使用 pthread_join() 函數(shù)來等待線程的完成。
當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
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.
更多實例參考:/co-3800.html