信号量与互斥锁性能对比
来源:互联网 发布:windows nginx 下载 编辑:程序博客网 时间:2024/04/30 16:02
/************************************************************************* > File Name: semaphore.c > Author: wangzhicheng > Mail: 2363702560@qq.com > Created Time: Sun 15 Feb 2015 09:37:23 AM WST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#include <sys/types.h>#include <time.h>uint n;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;sem_t sem_id;time_t start, finish;void *add_unlock(void *arg) {uint i;start = clock();for(i = 0;i < 1e6;i++) n++;finish = clock();return NULL;}void *add_mutex(void *arg) {start = clock();pthread_mutex_lock(&mutex);while(n < 1e6) n++;pthread_mutex_unlock(&mutex);finish = clock();return NULL;}void *add_sem(void *arg) {start = clock();sem_wait(&sem_id);while(n < 1e6) n++;sem_post(&sem_id);finish = clock();return NULL;}int main() {/* * single thread * *///add_unlock(NULL);//double elapse = (double)(finish - start) / CLOCKS_PER_SEC;//printf("elapse time = %lf\n", elapse); // 0.01s/* * 16 threads synchronize by mutex lock * *//*pthread_t thread_ids[16];int i;for(i = 0;i < 16;i++) {pthread_create(&thread_ids[i], NULL, add_mutex, NULL);}for(i = 0;i < 16;i++) {pthread_join(thread_ids[i], NULL);}double elapse = (double)(finish - start) / CLOCKS_PER_SEC;printf("elapse time = %lf\n", elapse); // 0.03s*//* * 16 threads synchronize by semaphore * *//*sem_init(&sem_id, 0, 1);pthread_t thread_ids[16];int i;for(i = 0;i < 16;i++) {pthread_create(&thread_ids[i], NULL, add_sem, NULL);}for(i = 0;i < 16;i++) {pthread_join(thread_ids[i], NULL);}double elapse = (double)(finish - start) / CLOCKS_PER_SEC;printf("elapse time = %lf\n", elapse);// 0.016s*/return 0;}
0 0
- 信号量与互斥锁性能对比
- 互斥体与信号量的对比
- 虚拟机性能测试与对比
- datediff 与 date_format 性能对比
- XmlDocumnt与XPathDocument性能对比
- currenthashmap 与 ehcache 性能对比
- log4j 与 logback 性能对比
- oracle与mysql性能对比
- pthread_spinlock_t与pthread_mutex_t性能对比
- 红黑树与小根堆性能对比
- Memcached与OCS性能对比
- ADO与EF性能对比
- Redis与Memcache性能对比
- scala与Java性能对比
- 信号量与互斥锁区别
- 信号量与互斥锁
- 信号量与互斥锁
- 关于互斥锁与信号量
- 系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
- 记Weblogic采用RAC方式链接数据库遇到的问题
- Server_如何从Apache官网下载windows版apache服务器;
- Perl 正则详述
- 3D赛瓦号——整装待发!
- 信号量与互斥锁性能对比
- 常用开发工具的密钥收藏
- 线段树总结
- leetcode_25_Reverse Nodes in k-Group
- Perl 懒惰匹配
- SHOI 2014 全
- Problem K: Cylinder
- 我为什么要求你用功读书
- [Android] ListView 滚回顶部