设为首页 加入收藏

TOP

C语言/原子/编译,你真的明白了吗?(二)
2017-10-16 18:19:06 】 浏览:9641
Tags:语言 原子 编译 真的 明白
; volatile int cnt = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* mythread(void* arg) { int i; for(i=0;i<500000000;i++) { pthread_mutex_lock(&mutex); cnt++; pthread_mutex_unlock(&mutex); } return NULL; } int main() { pthread_t id, id2; pthread_create(&id, NULL, mythread, NULL); pthread_create(&id2, NULL, mythread, NULL); pthread_join(id, NULL); pthread_join(id2, NULL); printf("cnt = %d\n", cnt); return 0; }

  测试一下

linux-p94b:/tmp/testhere # gcc -O2 test1.c -lpthread
linux-p94b:/tmp/testhere # for((i=0;i<10;i++));do ./a.out ; done
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000
cnt = 1000000000

  

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[小项目]行李箱(蓝牙解锁、称重) 下一篇文件输入输出(二):文件的操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目