linux c多線程 小例

來源:互聯網
上載者:User

通過建立兩個線程來實現對一個數的遞加。
或許這個執行個體沒有實際運用的價值,但是稍微改動一下,我們就可以用到其他地方去拉。

  1. #include <pthread.h>   
  2. #include <stdio.h>   
  3. #include <sys/time.h>   
  4. #include <string.h>   
  5. #include <unistd.h>   
  6. #define MAX 10   
  7. pthread_t thread[2];   
  8. pthread_mutex_t mut;   
  9. int number=0, i;   
  10. void *thread1(void *)   
  11. {   
  12.         printf ("thread1 : I'm thread 1\n");   
  13.         for (i = 0; i < MAX; i++)   
  14.         {   
  15.                 printf("thread1 : number = %d\n",number);   
  16.                 pthread_mutex_lock(&mut);   
  17.                         number++;   
  18.                 pthread_mutex_unlock(&mut);   
  19.                 sleep(2);   // 可採用pthread_delay_np函數是線程睡眠2秒
  20.         }   
  21.         printf("thread1 :the main function is waiting for me?\n");   
  22.         pthread_exit(NULL);   
  23. }   
  24. void *thread2(void *)   
  25. {   
  26.         printf("thread2 : I'm thread 2\n");   
  27.         for (i = 0; i < MAX; i++)   
  28.         {   
  29.                 printf("thread2 : number = %d\n",number);   
  30.                 pthread_mutex_lock(&mut);   
  31.                         number++;   
  32.                 pthread_mutex_unlock(&mut);   
  33.                 sleep(3);   
  34.         }   
  35.         printf("thread2 :the main function is waiting for me? \n");   
  36.         pthread_exit(NULL);   
  37. }   
  38. void thread_create(void)   
  39. {   
  40.         int temp;   
  41.         memset(&thread, 0, sizeof(thread));          //comment1   
  42.         /*Create thread*/  
  43.         if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2   
  44.                 printf("Thread 1 fail to create!\n");   
  45.         else  
  46.                 printf("Thread 1 created!\n");   
  47.         if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3   
  48.                 printf("Thread 2 fail to create!");   
  49.         else  
  50.                 printf("Thread 2 created!\n");   
  51. }   
  52. void thread_wait(void)   
  53. {   
  54.         /*Wait thread end*/  
  55.         if(thread[0] !=0) {                   //comment4   
  56.                 pthread_join(thread[0],NULL);   
  57.                 printf("Thread 1 completed\n");   
  58.         }   
  59.         if(thread[1] !=0) {                //comment5   
  60.                 pthread_join(thread[1],NULL);   
  61.                 printf("Thread 2 completed.\n");   
  62.         }   
  63. }   
  64. int main()   
  65. {   
  66.         /*init mutex*/  
  67.         pthread_mutex_init(&mut,NULL);   
  68.         printf("Main function,creating thread...\n");   
  69.         thread_create();   
  70.         printf("Main function,waiting for thread end....\n");   
  71.         thread_wait();   
  72.         return 0;   
  73. }

 

 

我是主函數哦,我正在建立線程,呵呵
線程1被建立
線程2被建立
我是主函數哦,我正在等待線程完成任務阿,呵呵
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 :主函數在等我完成任務嗎?
線程1已經結束
thread2 :主函數在等我完成任務嗎?
線程2已經結束

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.