zoukankan      html  css  js  c++  java
  • 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

    更多gcc提供的原子操作参考http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html

  • 相关阅读:
    Python字典
    Python集合
    Hungray匈牙利算法
    异常捕获模块,throw, try, catch, finally
    《加德纳艺术通史》罗杰克-劳利
    《高效休息法》久贺谷亮
    梯度检验 Gradient check,bias correction, Exponentially Weighted Averages
    方差与偏差,bias vs variance
    大数定理,中心极限定理以及一些常见分布
    聚类-均值漂移
  • 原文地址:https://www.cnblogs.com/feisky/p/2613921.html
Copyright © 2011-2022 走看看