zoukankan      html  css  js  c++  java
  • c多线程不加锁demo

    //
    // Created by gxf on 2019/12/13.
    //
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    int shareInt = 0;
    void increase_num(void);
    
    int main() {
        int ret;
        pthread_t thread1, thread2, thread3;
    
        ret = pthread_create(&thread1, NULL, increase_num, NULL);
        ret = pthread_create(&thread2, NULL, increase_num, NULL);
        ret = pthread_create(&thread3, NULL, increase_num, NULL);
    
        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
        pthread_join(thread3, NULL);
    
        printf("sharedInt:%d
    ", shareInt);
    
        return 0;
    }
    
    void increase_num(void) {
        long i, tmp;
        for (i=0; i <= 100000; i++) {
            tmp = shareInt;
            tmp = tmp + 1;
            shareInt = tmp;
        }
    }
    

      

  • 相关阅读:
    第一阶段冲刺02
    梦断代码阅读笔记01
    第一阶段冲刺01
    第十周总结报告
    单词统计
    用户模板分析
    第九周总结
    第九周课后作业
    py_11_ 0726
    Day_01
  • 原文地址:https://www.cnblogs.com/luckygxf/p/12032690.html
Copyright © 2011-2022 走看看