CAS锁与MUTEX锁性能测试
来源:互联网 发布:java项目流程怎么说 编辑:程序博客网 时间:2024/05/30 04:50
#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <sys/time.h>#define lock(lkp) do{ \ while(!__sync_bool_compare_and_swap(lkp, 0, 1)){ \ usleep(1000); \ } \}while(0)#define unlock(lkp) do{ \ *(lkp) = 0; \}while(0)static unsigned long long count = 0;static int lock = 0;struct timeval start, end;static pthread_mutex_t mutex;void *test_func(void *arg){ int i=0; for(i=0; i < 0xFFFFFFF; ++i) { lock(&lock); //pthread_mutex_lock(&mutex); count++; //pthread_mutex_unlock(&mutex); unlock(&lock); } return NULL;}int main(int argc, const char *argv[]){ pthread_t id[2]; int i = 0; gettimeofday(&start, NULL); for(i=0; i < 2; ++i) { pthread_create(&id[i],NULL,test_func,NULL); } for(i=0; i < 2; ++i) { pthread_join(id[i],NULL); } gettimeofday(&end, NULL); printf("waste time = %lus, %luus\n", end.tv_sec - start.tv_sec, end.tv_usec - start.tv_usec); printf("%llu\n",count); return 0;}
阅读全文
0 0
- CAS锁与MUTEX锁性能测试
- mutex 与 CAS
- 锁与CAS介绍
- 锁与CAS介绍
- CAS 与乐观锁
- mutex加锁和解锁性能测试
- 线程、同步与锁——Mutex
- 乐观锁与CAS操作
- Mutex与spin lock的性能对比
- linux下mutex与atomic性能比较
- linux下mutex与atomic性能比较
- Mutex测试
- CAS锁
- CAS原子操作实现无锁及性能分析
- CAS原子操作实现无锁及性能分析
- CAS原子操作实现无锁及性能分析
- CAS原子操作实现无锁及性能分析
- CAS原子操作实现无锁及性能分析
- Linux 基础:如何在Ubuntu上检查一个软件包是否安装
- 懒加载和预加载
- CSU1955: 一棵数据结构
- XListView的一些适配文件
- 使用apply实现bind方法
- CAS锁与MUTEX锁性能测试
- 事件深入应用二
- storm编程指南
- 集成聚类之EAC方法
- SSL P1963 提高组 俄罗斯方块
- web基础
- XListView的必须文件
- 炉石传说
- hdu 6119 小小粉丝度度熊(尺取)