原文: Linux C 多線程
linux下C語(yǔ)言多線程編程
#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 ; }
執(zhí)行結(jié)果
我是主函數(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é)束
下面一個(gè)稍微復(fù)雜的多線程
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)用它的線程將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源被收回。一個(gè)線程的結(jié)束有兩種途徑,一種是象我們上面的例子一樣,函數(shù)結(jié)束了,調(diào)用它的線程也就結(jié)束了;另一種方式是通過(guò)函數(shù)pthread_exit來(lái)實(shí)現(xiàn)。它的函數(shù)原型為:
extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
唯一的參數(shù)是函數(shù)的返回代碼,只要pthread_exit中的參數(shù)retval不是NULL,這個(gè)值將被傳遞給 thread_return。最后要說(shuō)明的是,一個(gè)線程不能被多個(gè)線程等待,否則第一個(gè)接收到信號(hào)的線程成功返回,其余調(diào)用pthread_join的線程則返回錯(cuò)誤代碼ESRCH。
實(shí)例:
#include <stdio.h> #include <pthread.h> #include <stdlib.h> pthread_t tid1, tid2; void * tret; void * thr_fn1( void * arg) { sleep( 1 ); // 睡眠一秒,等待TID2結(jié)束。 pthread_join(tid2, &tret); // tid1一直阻賽,等到tid2的退出,獲得TID2的退出碼 printf( " thread 2 exit code %d\n " , ( int )tret); printf( " thread 1 returning\n " ); return (( void *) 2 ); } void * thr_fn2( void * arg) { printf( " thread 2 exiting\n " ); pthread_exit(( void *) 3 ); } int main( void ) { int err; err = pthread_create(& tid1, NULL, thr_fn1, NULL); if (err != 0 ) printf( " can't create thread 1\n " ); err = pthread_create(& tid2, NULL, thr_fn2, NULL); if (err != 0 ) printf( " can't create thread 2\n " ); err = pthread_join(tid1, &tret); // 祝線程一直阻賽,等待TID1的返回。 if (err != 0 ) printf( " can't join with thread 1\n " ); printf( " thread 1 exit code %d\n " , ( int )tret); // err = pthread_join(tid2, &tret); // if (err != 0) // printf("can't join with thread 2\n"); // printf("thread 2 exit code %d\n", (int)tret); exit( 0 ); } 命令:#gcc -lthread myfile11- 3 .c :#. /a. out 運(yùn)行結(jié)果: thread 2 exiting thread 2 exit code 3 thread 1 returning thread 1 exit code 2
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
