您好,登錄后才能下訂單哦!
這篇“Linux多線程編程實(shí)例代碼分析”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Linux多線程編程實(shí)例代碼分析”文章吧。
下面先來(lái)一個(gè)實(shí)例。我們通過創(chuàng)建兩個(gè)線程來(lái)實(shí)現(xiàn)對(duì)一個(gè)數(shù)的遞加。或許這個(gè)實(shí)例沒有實(shí)際運(yùn)用的價(jià)值,但是稍微改動(dòng)一下,我們就可以用到其他地方去拉。
代碼:
/*thread_example.c : c multiple thread programming in linux *author : falcon *e-mail : tunzhj03@st.lzu.edu.cn */ #include <pthread.h> #include <stdio.h> #include <sys/time.h> #include <string.h> #define max 10 pthread_t thread[2]; pthread_mutex_t mut; int number=0, i; void *thread1() { printf ("thread1 : i'm thread 1/n"); for (i = 0; i < max; i++) { printf("thread1 : number = %d/n",number); pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(2); } printf("thread1 :主函數(shù)在等我完成任務(wù)嗎?/n"); pthread_exit(null); } void *thread2() { printf("thread2 : i'm thread 2/n"); for (i = 0; i < max; i++) { printf("thread2 : number = %d/n",number); pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(3); } printf("thread2 :主函數(shù)在等我完成任務(wù)嗎?/n"); pthread_exit(null); } void thread_create(void) { int temp; memset(&thread, 0, sizeof(thread)); //comment1 /*創(chuàng)建線程*/ if((temp = pthread_create(&thread[0], null, thread1, null)) != 0) //comment2 printf("線程1創(chuàng)建失敗!/n"); else printf("線程1被創(chuàng)建/n"); if((temp = pthread_create(&thread[1], null, thread2, null)) != 0) //comment3 printf("線程2創(chuàng)建失敗"); else printf("線程2被創(chuàng)建/n"); } void thread_wait(void) { /*等待線程結(jié)束*/ if(thread[0] !=0) { //comment4 pthread_join(thread[0],null); printf("線程1已經(jīng)結(jié)束/n"); } if(thread[1] !=0) { //comment5 pthread_join(thread[1],null); printf("線程2已經(jīng)結(jié)束/n"); } } int main() { /*用默認(rèn)屬性初始化互斥鎖*/ pthread_mutex_init(&mut,null); printf("我是主函數(shù)哦,我正在創(chuàng)建線程,呵呵/n"); thread_create(); printf("我是主函數(shù)哦,我正在等待線程完成任務(wù)阿,呵呵/n"); thread_wait(); return 0; }
下面我們先來(lái)編譯、執(zhí)行一下
引文:
falcon@falcon:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c falcon@falcon:~/program/c/code/ftp$ ./thread_example 我是主函數(shù)哦,我正在創(chuàng)建線程,呵呵 線程1被創(chuàng)建 線程2被創(chuàng)建 我是主函數(shù)哦,我正在等待線程完成任務(wù)阿,呵呵 thread1 : i'm thread 1 thread1 : number = 0 thread2 : i'm thread 2 thread2 : number = 1 thread1 : number = 2 thread2 : number = 3 thread1 : number = 4 thread2 : number = 5 thread1 : number = 6 thread1 : number = 7 thread2 : number = 8 thread1 : number = 9 thread2 : number = 10 thread1 :主函數(shù)在等我完成任務(wù)嗎? 線程1已經(jīng)結(jié)束 thread2 :主函數(shù)在等我完成任務(wù)嗎? 線程2已經(jīng)結(jié)束
實(shí)例代碼里頭的注釋應(yīng)該比較清楚了吧,下面我把網(wǎng)路上介紹上面涉及到的幾個(gè)函數(shù)和變量給引用過來(lái)。
引文:
線程相關(guān)操作
一 pthread_t
pthread_t在頭文件/usr/include/bits/pthreadtypes.h中定義:
typedef unsigned long int pthread_t;
它是一個(gè)線程的標(biāo)識(shí)符。
二 pthread_create
函數(shù)pthread_create用來(lái)創(chuàng)建一個(gè)線程,它的原型為:
extern int pthread_create __p ((pthread_t *__thread, __const pthread_attr_t *__attr,
void *(*__start_routine) (void *), void *__arg));
第一個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針,第二個(gè)參數(shù)用來(lái)設(shè)置線程屬性,第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址,最后一個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。這里,我們的函數(shù)thread不需要參數(shù),所以最后一個(gè)參數(shù)設(shè)為空指針。第二個(gè)參數(shù)我們也設(shè)為空指針,這樣將生成默認(rèn)屬性的線程。對(duì)線程屬性的設(shè)定和修改我們將在下一節(jié)闡述。當(dāng)創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說明創(chuàng)建線程失敗,常見的錯(cuò)誤返回代碼為eagain和einval。前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過多了;后者表示第二個(gè)參數(shù)代表的線程屬性值非法。創(chuàng)建線程成功后,新創(chuàng)建的線程則運(yùn)行參數(shù)三和參數(shù)四確定的函數(shù),原來(lái)的線程則繼續(xù)運(yùn)行下一行代碼。
三 pthread_join pthread_exit
函數(shù)pthread_join用來(lái)等待一個(gè)線程的結(jié)束。函數(shù)原型為:
extern int pthread_join __p ((pthread_t __th, void **__thread_return));
第一個(gè)參數(shù)為被等待的線程標(biāo)識(shí)符,第二個(gè)參數(shù)為一個(gè)用戶定義的指針,它可以用來(lái)存儲(chǔ)被等待線程的返回值。這個(gè)函數(shù)是一個(gè)線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源被收回。一個(gè)線程的結(jié)束有兩種途徑,一種是象我們上面的例子一樣,函數(shù)結(jié)束了,調(diào)用它的線程也就結(jié)束了;另一種方式是通過函數(shù)pthread_exit來(lái)實(shí)現(xiàn)。它的函數(shù)原型為:
extern void pthread_exit __p ((void *__retval)) __attribute__ ((__noreturn__));
唯一的參數(shù)是函數(shù)的返回代碼,只要pthread_join中的第二個(gè)參數(shù)thread_return不是null,這個(gè)值將被傳遞給 thread_return。最后要說明的是,一個(gè)線程不能被多個(gè)線程等待,否則第一個(gè)接收到信號(hào)的線程成功返回,其余調(diào)用pthread_join的線程則返回錯(cuò)誤代碼esrch。
在這一節(jié)里,我們編寫了一個(gè)最簡(jiǎn)單的線程,并掌握了最常用的三個(gè)函數(shù)pthread_create,pthread_join和pthread_exit。下面,我們來(lái)了解線程的一些常用屬性以及如何設(shè)置這些屬性。
互斥鎖相關(guān)
互斥鎖用來(lái)保證一段時(shí)間內(nèi)只有一個(gè)線程在執(zhí)行一段代碼。
一 pthread_mutex_init
函數(shù)pthread_mutex_init用來(lái)生成一個(gè)互斥鎖。null參數(shù)表明使用默認(rèn)屬性。如果需要聲明特定屬性的互斥鎖,須調(diào)用函數(shù) pthread_mutexattr_init。函數(shù)pthread_mutexattr_setpshared和函數(shù) pthread_mutexattr_settype用來(lái)設(shè)置互斥鎖屬性。前一個(gè)函數(shù)設(shè)置屬性pshared,它有兩個(gè)取值, pthread_process_private和pthread_process_shared。前者用來(lái)不同進(jìn)程中的線程同步,后者用于同步本進(jìn)程的不同線程。在上面的例子中,我們使用的是默認(rèn)屬性pthread_process_ private。后者用來(lái)設(shè)置互斥鎖類型,可選的類型有pthread_mutex_normal、pthread_mutex_errorcheck、 pthread_mutex_recursive和pthread _mutex_default。它們分別定義了不同的上所、解鎖機(jī)制,一般情況下,選用最后一個(gè)默認(rèn)屬性。
二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np
pthread_mutex_lock聲明開始用互斥鎖上鎖,此后的代碼直至調(diào)用pthread_mutex_unlock為止,均被上鎖,即同一時(shí)間只能被一個(gè)線程調(diào)用執(zhí)行。當(dāng)一個(gè)線程執(zhí)行到pthread_mutex_lock處時(shí),如果該鎖此時(shí)被另一個(gè)線程使用,那此線程被阻塞,即程序?qū)⒌却搅硪粋€(gè)線程釋放此互斥鎖。
注意:
1 需要說明的是,上面的兩處sleep不光是為了演示的需要,也是為了讓線程睡眠一段時(shí)間,讓線程釋放互斥鎖,等待另一個(gè)線程使用此鎖。下面的參考資料1里頭說明了該問題。但是在linux下好像沒有pthread_delay_np那個(gè)函數(shù)(我試了一下,提示沒有定義該函數(shù)的引用),所以我用了sleep來(lái)代替,不過參考資料2中給出另一種方法,好像是通過pthread_cond_timedwait來(lái)代替,里頭給出了一種實(shí)現(xiàn)的辦法。
2 請(qǐng)千萬(wàn)要注意里頭的注釋comment1-5,那是我花了幾個(gè)小時(shí)才找出的問題所在。
如果沒有comment1和comment4,comment5,將導(dǎo)致在pthread_join的時(shí)候出現(xiàn)段錯(cuò)誤,另外,上面的comment2和comment3是根源所在,所以千萬(wàn)要記得寫全代碼。因?yàn)樯厦娴木€程可能沒有創(chuàng)建成功,導(dǎo)致下面不可能等到那個(gè)線程結(jié)束,而在用pthread_join的時(shí)候出現(xiàn)段錯(cuò)誤(訪問了未知的內(nèi)存區(qū))。另外,在使用memset的時(shí)候,需要包含string.h頭文件哦
以上就是關(guān)于“Linux多線程編程實(shí)例代碼分析”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。