美文网首页
linux下锁/无锁性能比较

linux下锁/无锁性能比较

作者: perryn | 来源:发表于2017-02-16 10:33 被阅读239次

代码示例中三种类型:

    1.pthread_mutex_t,互斥锁
    2.__sync_add_and_fetch,GCC自带的原子锁
    3.nolock,无锁方式

代码如下:

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <time.h>
#define MAX_THD_SIZE 2048
uint64_t max = 0;
uint64_t sum = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static void incrment_with_lock (int *data)
{
        uint64_t i = 0;
        uint64_t count = max / MAX_THD_SIZE;
        for (; i < count; i++)
        {
                pthread_mutex_lock (&lock);
                (*data)++;
                pthread_mutex_unlock (&lock);
        }
}

static void incrment_with_nolock (int *data)
{
        uint64_t i = 0;
        uint64_t count = max / MAX_THD_SIZE;
        for (; i < count; i++)
        {
                (*data)++;
        }
}

static void incrment_with_atomic (int *data)
{
        uint64_t i = 0;
        uint64_t count = max / MAX_THD_SIZE;
        for (; i < count; i++)
        {
                __sync_add_and_fetch (data, 1);
        }
}

bool is_digit (const char *s)
{
        if (s == NULL)
        {
                return false;
        }
        while (*s != '\0')
        {
                if (isdigit (*(s++)) == 0)
                {
                        return false;
                }
        }
        return true;
}

int main (int argc, char *argv[])
{
        if (argc != 2 || !is_digit (argv[1]))
        {
                fprintf (stdout, "usage: %s number \n", argv[0]);
                return 0;
        }
        max = atoi (argv[1]);
        clock_t start, end;
        start = clock ();
        pthread_t thd[MAX_THD_SIZE];
        uint32_t i = 0;
#ifdef LOCK
        for (; i < MAX_THD_SIZE; i++)
        {
                pthread_create (&thd[i], NULL, (void *) &incrment_with_lock, (void *) &sum);
        }
        for (i = 0; i < MAX_THD_SIZE; i++)
        {
                pthread_join (thd[i], NULL);
        }
        end = clock ();
        fprintf (stdout, "sum = %d,incremnt_with_lock run time :%f s\n", sum, (double) (end - start) / CLOCKS_PER_SEC);
#endif
#ifdef ATOMIC
        for (; i < MAX_THD_SIZE; i++)
        {
                pthread_create (&thd[i], NULL, (void *) &incrment_with_atomic, (void *) &sum);
        }
        for (i = 0; i < MAX_THD_SIZE; i++)
        {
                pthread_join (thd[i], NULL);
        }
        end = clock ();
        fprintf (stdout, "sum = %d,incremnt_with_atomic run time :%f s\n", sum, (double) (end - start) / CLOCKS_PER_SEC);
#endif
#ifdef NOLOCK
        for (; i < MAX_THD_SIZE; i++)
        {
                pthread_create (&thd[i], NULL, (void *) &incrment_with_nolock, (void *) &sum);
        }
        for (i = 0; i < MAX_THD_SIZE; i++)
        {
                pthread_join (thd[i], NULL);
        }
        end = clock ();
        fprintf (stdout, "sum = %d,incremnt_with_nolock run time :%f s\n", sum, (double) (end - start) / CLOCKS_PER_SEC);
#endif
        return 0;
}

测试结果:

这里写图片描述

结果描述:

1.使用pthread_mutex_xxx类似的函数,针对多线程中操作一个变量,代价挺高,性能比较低。
2.不加锁这总方式,数据或错乱,但是性能是最佳的。
3.使用GCC原子锁,有一定的开销但是代价比使用pthread_mutex_xxx函数小。

相关文章

  • linux下锁/无锁性能比较

    代码示例中三种类型: 代码如下: 测试结果: 结果描述:

  • 世界上最简单的无锁哈希表

    无锁哈希表(Lock-Free Hash Table )可以提高多线程下的性能表现,但是因为实现一个无锁哈希表本身...

  • 6.数据库

    Android数据库ORM框架用法、源码和性能比较分析 synchronized与lock 对象锁、互斥锁、共享锁...

  • 无锁算法——CAS原理

    一、无锁算法 CAS(比较与交换,Compare and swap) 是一种有名的无锁算法。无锁编程,即不使用锁的...

  • Java 锁

    1:锁存在的问题1)在多线程竞争锁的情况下,加锁、释放锁会导致比较多的上下文切换和调度延时,引起性能问题。而且在上...

  • 自旋锁

    简单回顾一下CAS算法 CAS算法即compare and swap(比较与交换),是一种有名的无锁算法。无锁编程...

  • zookeeper和redis区别

    redis分布式锁,其实需要自己不断去尝试获取锁,比较消耗性能 zk分布式锁,获取不到锁,注册个监听器即可,不需要...

  • 锁分析(上)

    锁性能分析 iPhone 12真机测试,锁的性能数据对比图 性能从高到低排序:OSSpinLock(自旋锁)>os...

  • CAS原理分析

    锁机制存在问题 1.在多线程竞争下,加锁、释放锁会导致比较多的上下文切换和调度延时,引起性能问题。 2...

  • mysql锁

    mysql锁 性能:乐观锁,悲观锁 操作类型:读锁,写锁,都属于悲观锁 操作粒度:行锁,表锁 乐观锁:一种思想,通...

网友评论

      本文标题:linux下锁/无锁性能比较

      本文链接:https://www.haomeiwen.com/subject/ziicwttx.html