首页 > 编程语言 > 详细

c多线程不加锁demo

时间:2019-12-13 09:10:40      阅读:98      评论:0      收藏:0      [点我收藏+]
//
// 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\n", shareInt);

    return 0;
}

void increase_num(void) {
    long i, tmp;
    for (i=0; i <= 100000; i++) {
        tmp = shareInt;
        tmp = tmp + 1;
        shareInt = tmp;
    }
}

  

c多线程不加锁demo

原文:https://www.cnblogs.com/luckygxf/p/12032690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!