• 码海拾遗:Linux多线程mutex锁


      多线程计数,每个线程累加10个数。

      实现:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <errno.h>
      5 #include <pthread.h>
      6 
      7 pthread_mutex_t num_lock;
      8 
      9 int num = 0;
     10 int sum = 0;
     11 
     12 void* foo1()
     13 {
     14 #if 1
     15     pthread_mutex_lock(&num_lock);
     16     for(int i = 0;i<10;++i)
     17     {
     18         num += 1;
     19         sum += num;
     20         printf("thread 1,num = %d,sum = %d
    ",num,sum);
     21         usleep(10);
     22     }
     23     pthread_mutex_unlock(&num_lock);
     24 #else
     25     for(int i = 0;i<10;++i)
     26     {
     27         num += 1;
     28         sum += num;
     29         printf("thread 1,num = %d,sum = %d
    ",num,sum);
     30         usleep(10);
     31     }
     32 #endif
     33     return NULL;
     34 }
     35 
     36 void* foo2()
     37 {
     38 #if 1
     39     pthread_mutex_lock(&num_lock);
     40     for(int i = 0;i<10;++i)
     41     {
     42         num += 1;
     43         sum += num;
     44         printf("thread 2,num = %d,sum = %d
    ",num,sum);
     45         usleep(10);
     46     }
     47     pthread_mutex_unlock(&num_lock);
     48 #else
     49     for(int i = 0;i<10;++i)
     50     {
     51         num += 1;
     52         sum += num;
     53         printf("thread 2,num = %d,sum = %d
    ",num,sum);
     54         usleep(10);
     55     }
     56 #endif
     57     return NULL;
     58 }
     59 
     60 int main(int argc,char* argv[])
     61 {
     62     pthread_t tid1,tid2;
     63     int err;
     64 
     65     pthread_mutex_init(&num_lock,NULL);
     66 
     67     err = pthread_create(&tid1,NULL,foo1,NULL);
     68     if(err != 0)
     69     {
     70         printf("error code:%d,reason:%s
    ",err,strerror(err));
     71         return -1;
     72     }
     73 
     74     err = pthread_create(&tid2,NULL,foo2,NULL);
     75     if(err != 0)
     76     {
     77         printf("error code:%d,reason:%s
    ",err,strerror(err));
     78         return -1;
     79     }
     80 
     81     //for(int i = 0;i<10;++i)
     82     //    printf("main thread, num = %d
    ",num++);
     83 
     84     err = pthread_join(tid1,NULL);
     85     if(err != 0)
     86     {
     87         printf("error code:%d,reason:%s
    ",err,strerror(err));
     88         return -1;
     89     }
     90     err = pthread_join(tid2,NULL);
     91     if(err != 0)
     92     {
     93         printf("error code:%d,reason:%s
    ",err,strerror(err));
     94         return -1;
     95     }
     96 
     97     pthread_mutex_destroy(&num_lock);
     98 
     99     return 0;
    100 }
  • 相关阅读:
    思考未来:你的目标是什么
    从非同凡响开始:绝不要做他人也在做的事
    Elasticsearch的内置分词器
    Elasticsearch倒排索引的核心组成
    Session 与 JWT
    vue全屏组件
    css弹性盒骰子
    es6模块化
    移动端适配
    echarts-3D地图
  • 原文地址:https://www.cnblogs.com/lianshuiwuyi/p/7673807.html
Copyright © 2020-2023  润新知