gcc原子操作与spinlock简单对比
来源:互联网 发布:杨霞sunny扒皮知乎 编辑:程序博客网 时间:2024/06/06 04:23
GCC 提供的原子操作
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作。
type __sync_fetch_and_add (type *ptr, type value, ...)
type __sync_fetch_and_sub (type *ptr, type value, ...)
type __sync_fetch_and_or (type *ptr, type value, ...)
type __sync_fetch_and_and (type *ptr, type value, ...)
type __sync_fetch_and_xor (type *ptr, type value, ...)
type __sync_fetch_and_nand (type *ptr, type value, ...)
type __sync_add_and_fetch (type *ptr, type value, ...)
type __sync_sub_and_fetch (type *ptr, type value, ...)
type __sync_or_and_fetch (type *ptr, type value, ...)
type __sync_and_and_fetch (type *ptr, type value, ...)
type __sync_xor_and_fetch (type *ptr, type value, ...)
type __sync_nand_and_fetch (type *ptr, type value, ...)
先看看测试代码
// cas.c#include <stdio.h>#include <pthread.h>#include <stdlib.h>static int count = 0;void *test_func(void *arg){ int i=0; for(i=0;i<100000;++i){ __sync_fetch_and_add(&count,1); } return NULL;}int main(int argc, const char *argv[]){ pthread_t id[100]; int i = 0; for(i=0;i<100;++i){ pthread_create(&id[i],NULL,test_func,NULL); } for(i=0;i<100;++i){ pthread_join(id[i],NULL); } printf("%d\n",count); return 0;}
// spinlock.c#include <stdio.h>#include <pthread.h>#include <stdlib.h>static int count = 0;static pthread_spinlock_t spinlock;void *test_func(void *arg){ int i=0; for(i=0;i<100000;++i){ pthread_spin_lock (&spinlock); count++; pthread_spin_unlock(&spinlock); } return NULL;}int main(int argc, const char *argv[]){ pthread_t id[100]; int i = 0; pthread_spin_init (&spinlock, 0); for(i=0;i<100;++i){ pthread_create(&id[i],NULL,test_func,NULL); } for(i=0;i<100;++i){ pthread_join(id[i],NULL); } printf("%d\n",count); return 0;}
// mutex.c #include <stdio.h>#include <pthread.h>#include <stdlib.h>static int count = 0;static pthread_mutex_t mutex;void *test_func(void *arg){ int i=0; for(i=0;i<100000;++i){ pthread_mutex_lock (&mutex); count++; pthread_mutex_unlock(&mutex); } return NULL;}int main(int argc, const char *argv[]){ pthread_t id[100]; int i = 0; pthread_mutex_init (&mutex,NULL); for(i=0;i<100;++i){ pthread_create(&id[i],NULL,test_func,NULL); } for(i=0;i<100;++i){ pthread_join(id[i],NULL); } printf("%d\n",count); return 0;}
结果:
# time ./mutex 10000000real 0m0.235suser 0m0.040ssys 0m0.000s# time ./spinlock 10000000real 0m0.111suser 0m0.010ssys 0m0.010s# time ./cas 10000000real 0m0.083suser 0m0.010ssys 0m0.000s
更多gcc提供的原子操作参考http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
- gcc原子操作与spinlock简单对比
- gcc原子操作与spinlock简单对比
- gcc 原子操作 与 volatile
- Linux并发与同步(一)原子操作/spinlock/mutex
- 简单的GCC spinlock
- spinlock与mutex对比
- linux gcc原子操作
- gcc的原子操作
- gcc 原子操作函数
- gcc原子化操作
- gcc 原子操作
- GCC的原子操作
- gcc 原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- live555学习笔记15-RTCPInstance类小结
- 容器赋值 容器使用问题:vector subscript out of range
- hadoop上优化速度可以采用改变outputDefinition为avro的方法
- karma 自动化单元测试 Jasmine
- 以太网端口在不发送数据的时候,波形是什么样子
- gcc原子操作与spinlock简单对比
- Delphi 与 VC 共享接口和对象
- poj 2777 Count Color(线段树)
- html5学习笔记一:html5简介,html5和html有什么不一样。
- 很不错的java基础教程
- android开发之旅-------Log的使
- makefile 格式注意事项
- 【computer vision】基于粒子滤波的物体跟踪
- sicly 1381之预处理