linux下鎖/無鎖性能比較

代碼示例中三種類型:
        1.pthread_mutex_t,互斥鎖
        2.__sync_add_and_fetch,GCC自帶的原子鎖
        3.nolock,無鎖方式
代碼如下:
/*************************************************************************
        > File Name: log_test.c
        > Author: perrynzhou
        > Mail: [email protected]
        > 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函數小。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章