zoukankan      html  css  js  c++  java
  • linux下多线程编程

    先看执行的结果:

     1 主函数正在创建线程,...
     2 线程1被创建
     3 Thread1 : I'm thread 1th
     4 线程2被创建
     5 Thread2 : I'm thread 2nd
     6 thread2 : number = 0
     7 线程3被创建
     8 主函数正在等待线程结束...
     9 thread1 : number = 0
    10 Thread3 : I'm thread 3nd
    11 thread3 : number = 2
    12 thread3 : number = 3
    13 thread2 : number = 4
    14 thread1 : number = 5
    15 thread3 : number = 6
    16 thread2 : number = 7
    17 thread3 : number = 8
    18 thread1 : number = 9
    19 thread3 : number = 10
    20 thread2 : number = 11
    21 thread3 : number = 12
    22 thread2 : number = 13
    23 thread1 : number = 14
    24 thread3 : number = 15
    25 thread3 : number = 16
    26 thread2 : number = 17
    27 thread1 : number = 18
    28 thread3 : number = 19
    29 thread2 : number = 20
    30 thread3 : number = 21
    31 thread1 : number = 22
    32 thread3 : number = 23
    33 thread2 : number = 24
    34 thread3 : number = 25
    35 thread2 : number = 26
    36 thread1 : number = 27
    37 thread3 : number = 28
    38 thread3 : number = 29
    39 thread2 : number = 30
    40 thread1 : number = 31
    41 Thread3 :main函数在等我完成任务吗?
    42 Thread2 :main函数在等我完成任务吗?
    43 Thread1 :main函数在等我完成任务吗?
    44 线程1已经结束
    45 线程2已经结束
    46 线程3已经结束

    代码如下:参考网上的,稍微修改了下:否则,不能编译和运行:

      1 //fileName: threadSample.c
      2 #include <pthread.h>
      3 #include <stdio.h>
      4 #include <sys/time.h>
      5 #include <string.h>
      6 #include <unistd.h>                        //添加的头文件
      7 #define MAX 30
      8 
      9 pthread_t thread[3];  //3个线程
     10 pthread_mutex_t mut;
     11 int number=0;
     12 int i;
     13 
     14 void *thread1(){
     15         printf ("Thread1 : I'm thread 1th
    ");
     16         for (i = 0; i < MAX; i++)
     17         {
     18                 printf("thread1 : number = %d
    ",number);
     19                 pthread_mutex_lock(&mut);
     20                         number++;
     21                 pthread_mutex_unlock(&mut);
     22                 sleep(4);
     23         }
     24 
     25         printf("Thread1 :main函数在等我完成任务吗?
    ");
     26         pthread_exit(NULL);
     27 }
     28 
     29 void *thread2(){
     30         printf("Thread2 : I'm thread 2nd
    ");
     31 
     32         for (i = 0; i < MAX; i++)
     33         {
     34                 printf("thread2 : number = %d
    ",number);
     35                 pthread_mutex_lock(&mut);
     36                         number++;
     37                 pthread_mutex_unlock(&mut);
     38                 sleep(3);
     39         }
     40 
     41         printf("Thread2 :main函数在等我完成任务吗?
    ");
     42         pthread_exit(NULL);
     43 }
     44 
     45 void *thread3(){
     46         printf("Thread3 : I'm thread 3nd
    ");
     47 
     48         for (i = 0; i < MAX; i++)
     49         {
     50                 printf("thread3 : number = %d
    ",number);
     51                 pthread_mutex_lock(&mut);
     52                         number++;
     53                 pthread_mutex_unlock(&mut);
     54                 sleep(2);
     55         }
     56 
     57         printf("Thread3 :main函数在等我完成任务吗?
    ");
     58         pthread_exit(NULL);
     59 }
     60 
     61 void thread_create(void){                                                                  int temp;
     62         memset(&thread, 0, sizeof(thread));
     63         /*创建线程*/
     64         if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
     65                 printf("线程1创建失败!
    ");
     66         else
     67                 printf("线程1被创建
    ");
     68 
     69         if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
     70                 printf("线程2创建失败");
     71         else
     72                 printf("线程2被创建
    ");
     73 
     74         if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0)
     75                 printf("线程3创建失败");
     76         else
     77                 printf("线程3被创建
    ");
     78 }
     79 
     80 void thread_wait(void){
     81         /*等待线程结束*/
     82         if(thread[0] !=0){
     83                 pthread_join(thread[0],NULL);
     84                 printf("线程1已经结束
    ");
     85         }
     86         if(thread[1] !=0){
     87                pthread_join(thread[1],NULL);
     88                printf("线程2已经结束
    ");
     89         }
     90         if(thread[2] !=0){
     91                pthread_join(thread[2],NULL);
     92                printf("线程3已经结束
    ");
     93         }
     94 }
     95 
     96 int main()
     97 {
     98         /*用默认属性初始化互斥锁*/
     99         pthread_mutex_init(&mut,NULL);
    100 
    101         printf("主函数正在创建线程,...
    ");
    102         thread_create();
    103         printf("主函数正在等待线程结束...
    ");
    104         thread_wait();
    105 
    106         return 0;
    107 }

    再次执行结果为:

     1 主函数正在创建线程,...
     2 线程1被创建
     3 Thread1 : I'm thread 1th
     4 thread1 : number = 0
     5 Thread2 : I'm thread 2nd
     6 thread2 : number = 1
     7 线程2被创建
     8 线程3被创建
     9 主函数正在等待线程结束...
    10 Thread3 : I'm thread 3nd
    11 thread3 : number = 2
    12 thread3 : number = 3
    13 thread2 : number = 4
    14 thread1 : number = 5
    15 thread3 : number = 6
    16 thread2 : number = 7
    17 thread3 : number = 8
    18 thread1 : number = 9
    19 thread3 : number = 10
    20 thread2 : number = 11
    21 thread3 : number = 12
    22 thread1 : number = 13
    23 thread2 : number = 14
    24 thread3 : number = 15
    25 thread3 : number = 16
    26 thread2 : number = 17
    27 thread1 : number = 18
    28 thread3 : number = 19
    29 thread2 : number = 20
    30 thread3 : number = 21
    31 thread1 : number = 22
    32 thread3 : number = 23
    33 thread2 : number = 24
    34 thread3 : number = 25
    35 thread1 : number = 26
    36 thread2 : number = 27
    37 thread3 : number = 28
    38 thread3 : number = 29
    39 thread2 : number = 30
    40 thread1 : number = 31
    41 Thread3 :main函数在等我完成任务吗?
    42 Thread2 :main函数在等我完成任务吗?
    43 Thread1 :main函数在等我完成任务吗?
    44 线程1已经结束
    45 线程2已经结束
    46 线程3已经结束

    编译方法:gcc threadSample.c -o threadSample -lpthread

    代码非常简单,请自行理解吧

  • 相关阅读:
    Ural 1201 Which Day Is It? 题解
    Ural 1250 Sea Burial 题解
    2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记
    Codeforces 718A Efim and Strange Grade 程序分析
    CentOS7 修改MySql默认端口
    Ubuntu 18.04 版本中安装mysql 8的方法
    NET_NET深入体验与实战 第一章 .NET你知道 1.1什么是 .NET
    第五课
    c# 第一节课 一些简单的应用
    MDI窗体和窗体之间的操作总结
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/12584529.html
Copyright © 2011-2022 走看看