linux下锁/无锁性能比较

代码示例中三种类型:
        1.pthread_mutex_t,互斥锁
        2.__sync_add_and_fetch,GCC自带的原子锁
        3.nolock,无锁方式
代码如下:
/*************************************************************************
        > File Name: log_test.c
        > Author: perrynzhou
        > Mail: 715169549@qq.com
        > Created Time: Fri 13 Jan 2017 05:00:46 PM HKT
 ************************************************************************/

#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函数小。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值