国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
linux下的POSIX C 多線程編程 helloworld
http://blog.csdn.net/kennyrose/article/details/7545424
2012
  1. #include<pthread.h>  
  2. #include<stdio.h>  
  3. #include<unistd.h>  
  4. #include<string.h>  
  5.   
  6. #define MAX 10  
  7.   
  8. pthread_t thread[2];  
  9. pthread_mutex_t mut;  
  10. int number = 0;  
  11. int i;  
  12.   
  13. void* thread1( void *param )  
  14. {  
  15.         printf( "thread1: I'm thread 1\n " );  
  16.         for( i = 0; i < MAX; i++ )  
  17.         {  
  18.                 printf( "thread1: number = %d\n", number );  
  19.                 pthread_mutex_lock( &mut );  
  20.                         ++number;  
  21.                 pthread_mutex_unlock( &mut );  
  22.                 sleep( 2 );  
  23.         }  
  24.   
  25.         printf( "thread1: 主函數(shù)等我完成任務(wù)嗎? \n " );  
  26.         pthread_exit( NULL );  
  27.   
  28.         return ( void* )0;  
  29. }  
  30.   
  31. void* thread2( void* param )  
  32. {  
  33.         printf( "thread2: I'm thread 2\n" );  
  34.         for( i = 0; i < MAX; i++ )  
  35.         {  
  36.                 printf( "thread2: number = %d\n", number );  
  37.                 pthread_mutex_lock( &mut );  
  38.                         ++number;  
  39.                 pthread_mutex_unlock( &mut );  
  40.                 sleep( 3 );  
  41.         }  
  42.   
  43.         printf( "thread2: 主函數(shù)在等我完成任務(wù)嗎? \n" );  
  44.         pthread_exit( NULL );  
  45.   
  46.         return ( void* )0;  
  47. }  
  48.   
  49. void thread_create( void )  
  50. {  
  51.         int temp;  
  52.         memset( &thread, 0, sizeof( thread ) );  
  53.   
  54.         // 創(chuàng)建線程1  
  55.         if( ( temp = pthread_create( &thread[0], NULL, thread1, NULL ) ) != 0 )  
  56.         {  
  57.                 printf( "線程1創(chuàng)建失敗!\n" );  
  58.         }  
  59.         else  
  60.         {  
  61.                 printf( "線程1被創(chuàng)建\n" );  
  62.         }  
  63.   
  64.         // 創(chuàng)建線程2  
  65.         if( ( temp = pthread_create( &thread[1], NULL, thread2, NULL ) ) != 0 )  
  66.         {  
  67.                 printf( "線程2創(chuàng)建失敗!\n" );  
  68.         }  
  69.         else  
  70.         {  
  71.                 printf( "線程2被創(chuàng)建\n" );  
  72.         }  
  73. }  
  74.   
  75. void thread_wait( void )  
  76. {  
  77.         // 主函數(shù)等待線程1結(jié)束  
  78.         if ( thread[0] != 0 )  
  79.         {  
  80.                 pthread_join( thread[0], NULL );  
  81.                 printf( "線程1已經(jīng)結(jié)束\n" );  
  82.         }  
  83.   
  84.         // 主函數(shù)等待線程2結(jié)束  
  85.         if ( thread[1] != 0 )  
  86.         {  
  87.                 pthread_join( thread[1], NULL );  
  88.                 printf( "線程2已經(jīng)結(jié)束\n" );  
  89.         }  
  90. }  
  91.   
  92. int main()  
  93. {  
  94.         // 初始化互斥鎖  
  95.         pthread_mutex_init( &mut, NULL );  
  96.   
  97.         printf( "我是main函數(shù),正在創(chuàng)建線程...\n" );  
  98.         thread_create();  
  99.   
  100.         printf( "我是主函數(shù),正在等待線程完成任務(wù)...\n" );  
  101.         thread_wait();  
  102.   
  103.         printf( "main函數(shù)已退出\n" );  
  104.         return 0;  
  105. }  

// main output1 

  1. 我是main函數(shù),正在創(chuàng)建線程...  
  2. 線程1被創(chuàng)建  
  3. 線程2被創(chuàng)建  
  4. 我是主函數(shù),正在等待線程完成任務(wù)...  
  5. thread1: I'm thread 1  
  6.  thread2: I'm thread 2  
  7. thread1: number = 0  
  8. thread2: number = 0  
  9. thread1: number = 2  
  10. thread2: number = 3  
  11. thread1: number = 4  
  12. thread2: number = 5  
  13. thread1: number = 6  
  14. thread1: number = 7  
  15. thread2: number = 8  
  16. thread1: number = 9  
  17. thread2: number = 10  
  18. thread1: 主函數(shù)等我完成任務(wù)嗎?  
  19.  線程1已經(jīng)結(jié)束  
  20. thread2: 主函數(shù)在等我完成任務(wù)嗎?  
  21. 線程2已經(jīng)結(jié)束  
  22. main函數(shù)已退出  

// main output2

  1. 我是main函數(shù),正在創(chuàng)建線程...  
  2. 線程1被創(chuàng)建  
  3. 線程2被創(chuàng)建  
  4. 我是主函數(shù),正在等待線程完成任務(wù)...  
  5. thread1: I'm thread 1  
  6.  thread1: number = 0  
  7. thread2: I'm thread 2  
  8. thread2: number = 1  
  9. thread1: number = 2  
  10. thread2: number = 3  
  11. thread1: number = 4  
  12. thread2: number = 5  
  13. thread1: number = 6  
  14. thread1: number = 7  
  15. thread2: number = 8  
  16. thread1: number = 9  
  17. thread2: number = 10  
  18. thread1: 主函數(shù)等我完成任務(wù)嗎?  
  19.  線程1已經(jīng)結(jié)束  
  20. thread2: 主函數(shù)在等我完成任務(wù)嗎?  
  21. 線程2已經(jīng)結(jié)束  
  22. main函數(shù)已退出  


g++編譯命令如下: g++ -lpthread -o helloworld.out helloworld.cpp


問題:兩個(gè)線程共同完成任務(wù),但是對(duì)于共享變量 i 沒有互斥加鎖操作,所以以上多線程程序還是存在問題的!


為了達(dá)到兼容可移植的目的,POSIX給出了C語言下的多線程編程庫 pthread,具體的學(xué)習(xí)資料請(qǐng)參考一下鏈接

http://blog.csdn.net/future_fighter/article/details/3865071

http://blog.csdn.net/sunboy_2050/article/details/5921003

http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
多線程編程實(shí)例---pthread_join函數(shù)詳解
linux下C語言多線程編程實(shí)例
pthread
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服