Linux中多線程詳解及簡單實例
Linux中多線程詳解及簡單實例
1.概念
進程:運行中的程序。
線程:一個程序中的多個執(zhí)行路徑。更準(zhǔn)確的定義是:線程是一個進程內(nèi)部的一個控制序列。
2.為什么要有線程?
用fork調(diào)用進程代價太高,需要讓一個進程同時做多件事情,線程就非常有用。
3.線程的優(yōu)點和缺點。
優(yōu)點:
(1)有時,讓程序看起來是在同時做兩件事是非常有用的。 比如在編輯文檔時,還能統(tǒng)計文檔里的單詞個數(shù)。
(2)一個混雜著輸入、計算、輸出的程序,利用線程可以將這3個部 分分成3個線程來執(zhí)行,從而改變程序執(zhí)行的性能。
(3)一般來說,線程之間切換需要操作系統(tǒng)所做的工作比進程間切換需要的代價小。
缺點:
(1)編寫線程需要非常仔細的設(shè)計。
(2)對多線程的調(diào)試困難程度比單線程調(diào)試大得多。
4.創(chuàng)建線程
#include <pthread.h> (1)int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start_routine)(void *),void *arg); pthread_t pthread_self(void); (2)int pthread_equal(pthread_t thread1,pthread_t thread2); (3)int pthread_once(pthread_once_t *once_control,void(*init_routine)(void));
Linux系統(tǒng)支持POSIX多線程接口,稱為pthread。編寫linux下的多線程程序,需要包含頭文件pthread.h,鏈接時需要使用庫libpthread.a。
如果在主線程里面創(chuàng)建線程,程序就會在創(chuàng)建線程的地方產(chǎn)生分支,變成兩個部分執(zhí)行。線程的創(chuàng)建通過函數(shù)pthread_create來完成。成功返回0。
1.線程創(chuàng)建: int pthread_create(pthread_t thread,pthread_attr_t *attr,void (start_routine)(void ),void *arg); pthread_t pthread_self(void); 參數(shù)說明: thread:指向pthread_create類型的指針,用于引用新創(chuàng)建的線程。 attr:用于設(shè)置線程的屬性,一般不需要特殊的屬性,所以可以簡單地設(shè)置為NULL。 (start_routine)(void ):傳遞新線程所要執(zhí)行的函數(shù)地址。 arg:新線程所要執(zhí)行的函數(shù)的參數(shù)。 調(diào)用如果成功,則返回值是0,如果失敗則返回錯誤代碼。 2.線程終止 void pthread_exit(void *retval); 參數(shù)說明: retval:返回指針,指向線程向要返回的某個對象。 線程通過調(diào)用pthread_exit函數(shù)終止執(zhí)行,并返回一個指向某對象的指針。注意:絕不能用它返回一個指向局部變量的指針,因為線程調(diào)用該函數(shù)后,這個局部變量就不存在了,這將引起嚴重的程序漏洞。 3.線程同步 int pthread_join(pthread_t th, void **thread_return); 參數(shù)說明: th:將要等待的線程,線程通過pthread_create返回的標(biāo)識符來指定。 thread_return:一個指針,指向另一個指針,而后者指向線程的返回值。
一個簡單的創(chuàng)建多線程的程序:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void *thread_function(void *arg); char message[] = "Hello World"; int main() { int res; pthread_t a_thread; void *thread_result; res = pthread_create(&a_thread, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed!"); exit(EXIT_FAILURE); } printf("Waiting for thread to finish.../n"); res = pthread_join(a_thread, &thread_result); if (res != 0) { perror("Thread join failed!/n"); exit(EXIT_FAILURE); } printf("Thread joined, it returned %s/n", (char *)thread_result); printf("Message is now %s/n", message); exit(EXIT_FAILURE); } void *thread_function(void *arg) { printf("thread_function is running. Argument was %s/n", (char *)arg); sleep(3); strcpy(message, "Bye!"); pthread_exit("Thank you for your CPU time!"); }
輸出結(jié)果
$./thread1[輸出]: thread_function is running. Argument was Hello World Waiting for thread to finish... Thread joined, it returned Thank you for your CPU time! Message is now Bye!
以上就是Linux 多線程的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
linux腳本實現(xiàn)自動發(fā)送和收取郵件的設(shè)置方法
這篇文章主要是介紹linux下通過腳本自動發(fā)送和收取郵件的設(shè)置方法,有需要的朋友可以參考下2013-05-05給定鏈表中間節(jié)點指針,刪除中間節(jié)點的方法
本文實現(xiàn)算法來刪除單鏈表中的中間節(jié)點,只知道指向該節(jié)點中間節(jié)點的指針,大家可以參考使用2013-11-11Shell之function函數(shù)的定義及調(diào)用示例
本文主要介紹了Shell之function函數(shù)的定義及調(diào)用示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Linux中多命令執(zhí)行'';''和''&&''的區(qū)別解釋
大家有沒有發(fā)現(xiàn)在 Linux 中經(jīng)常使用到一個命令,如 make && make install,這里也可以使用 make ; make install,那么在 Linux 中執(zhí)行命令 ; 和 && 有何區(qū)別?下面通過這篇文章來給大家詳細的介紹下面,有需要的朋友們可以參考借鑒。2016-12-12Linux下查找后門程序 CentOS 查后門程序的shell腳本
這篇文章主要介紹了Linux下查找后門程序 CentOS 查后門程序的shell腳本,需要的朋友可以參考下2014-09-09使用shell腳本實現(xiàn)ping對應(yīng)IP所對應(yīng)的人名(推薦)
這篇文章主要介紹了使用shell腳本實現(xiàn)ping對應(yīng)IP所對應(yīng)的人名的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11