Linux c的多线程编程实例

通过创建两个线程来实现对一个数的递加。
或许这个实例没有实际运用的价值,但是稍微改动一下,我们就可以用到其他地方去拉。

  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);   
  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已经结束

猜你喜欢

转载自lgzdlmu.iteye.com/blog/1306528