码迷,mamicode.com
首页 > 其他好文 > 详细

gcc原子操作测试

时间:2019-11-29 15:47:20      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:static   tail   stat   article   null   链接   char   art   join   

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <stdlib.h>
 4  
 5 static int count = 0;
 6  
 7 void *test_func(void *arg)
 8 {
 9     int i=0;
10     for(i=0; i < 20000; i++) {
11         __sync_fetch_and_add(&count,1);
12         //count++;
13     }
14     return NULL;
15 }
16  
17 int main(int argc, const char *argv[])
18 {
19     pthread_t id[20];
20     int i = 0;
21  
22     for(i=0; i < 20; i++) {
23         pthread_create(&id[i],NULL,test_func,NULL);
24     }
25  
26     for(i=0; i<20; ++i) {
27         pthread_join(id[i],NULL);
28     }
29  
30     printf("%d\n",count);
31     return 0;
32 }

参考链接:https://blog.csdn.net/youfuchen/article/details/23179799

gcc原子操作测试

标签:static   tail   stat   article   null   链接   char   art   join   

原文地址:https://www.cnblogs.com/zengjianrong/p/11958034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!