gcc原子操作与spinlock简单对比

简介:

先看看测试代码

复制代码
// 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                            
10000000
real    0m0.235s
user    0m0.040s
sys     0m0.000s

# time ./spinlock 
10000000
real    0m0.111s
user    0m0.010s
sys     0m0.010s

# time ./cas 
10000000
real    0m0.083s
user    0m0.010s
sys     0m0.000s


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2012/07/29/2613921.html,如需转载请自行联系原作者

相关文章
|
编译器 Linux 开发工具
|
NoSQL 编译器 开发工具
006.gcc编译器
gcc是什么?
164 0
006.gcc编译器
|
存储 NoSQL 算法
从一个crash问题展开,探索gcc编译优化细节
问题分析的过程也正是技术成长之路,本文以一个gcc编译优化引发的crash为切入点,逐步展开对编译器优化细节的探索之路,在分析过程中打开了新世界的大门……
|
8月前
|
存储 编译器 C语言
深入理解GCC 和 G++ 编译器
GCC 和 G++ 是 GNU 工具链中的核心编译器,支持 C 和 C++ 程序开发。本文详细介绍其编译流程、常用选项及动态链接与静态链接的区别。编译过程分为预处理、编译、汇编和链接四个阶段,每个阶段有特定任务和命令选项。常用选项如 `-E`、`-S`、`-c` 和 `-o` 分别用于预处理、生成汇编代码、生成目标文件和指定输出文件。动态链接节省空间且易于更新,但依赖运行时库;静态链接独立高效,但文件较大且更新困难。合理选择优化选项(如 `-O0` 至 `-O3`)可提升程序性能。掌握这些知识有助于开发者更高效地编写、调试和优化代码。
370 23
深入理解GCC 和 G++ 编译器
|
前端开发 C语言
gcc动态库升级
gcc动态库升级
|
11月前
|
编译器 Linux C语言
gcc的编译过程
GCC(GNU Compiler Collection)的编译过程主要包括四个阶段:预处理、编译、汇编和链接。预处理展开宏定义,编译将代码转换为汇编语言,汇编生成目标文件,链接将目标文件与库文件合并成可执行文件。
335 11
|
编译器 开发工具 C语言
Gcc 链接文件
Gcc 链接文件
111 4
|
编译器 C语言 C++
MinGW安装gcc
MinGW安装gcc
340 0
|
C语言
gcc的简易用法
【5月更文挑战第10天】gcc的简易用法。
130 8
|
C语言
gcc的简易用法(编译、参数与链接)
【5月更文挑战第14天】gcc的简易用法(编译、参数与链接)。
127 1