C++ mutex, atomic, CAS性能

1 篇文章 0 订阅
1 篇文章 0 订阅
#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>
#include <functional>
#include <future>



static uint64_t total_1 = 0;
std::mutex mtx;

void pred1(void)
{
    for (uint64_t i = 0; i <= 100000000; i++) {
        mtx.lock();
        total_1 += i;
        mtx.unlock();
    }
}

void test1()
{
    total_1 = 0;
    std::cout << "thread[" << std::this_thread::get_id() << "]-1 start...\n";

    auto start = std::chrono::steady_clock::now();

    std::thread t1(pred1);
    std::thread t2(pred1);

    t1.join();
    t2.join();

    auto end = std::chrono::steady_clock::now();

    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
    std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}

std::atomic_uint64_t total_2{ 0 };

void pred2(void)
{
    for (uint64_t i = 0; i <= 100000000; i++) {
        total_2 += i;
    }
}

void test2()
{
    std::cout << "thread[" << std::this_thread::get_id() << "]-2 start...\n";

    auto start = std::chrono::steady_clock::now();

    std::thread t1(pred2);
    std::thread t2(pred2);

    t1.join();
    t2.join();

    auto end = std::chrono::steady_clock::now();

    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(ms): "   << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
    std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(s): "   << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}

std::atomic<uint64_t> total_3;
void pred3(void)
{
    uint64_t expect = 0;
    uint64_t desired = 0;

    for (uint64_t i = 0; i <= 100000000; i++) {
        do
        {
            expect = total_3.load();
            desired = expect + i;
        } while (!total_3.compare_exchange_weak(expect, desired, std::memory_order_release, std::memory_order_relaxed));
        //} while (!total_3.compare_exchange_strong(expect, desired, std::memory_order_release, std::memory_order_relaxed));
    }
}

void test3()
{
    std::cout << "thread[" << std::this_thread::get_id() << "]-3 start...\n";

    auto start = std::chrono::steady_clock::now();

    std::thread t1(pred3);
    std::thread t2(pred3);

    t1.join();
    t2.join();

    auto end = std::chrono::steady_clock::now();

    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
    std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}

void tesxxx()
{
    total_1 = 0;
    std::cout << "tesxxx[" << std::this_thread::get_id() << "]-x start...\n";

    auto start = std::chrono::steady_clock::now();
    pred1();
    auto end = std::chrono::steady_clock::now();

    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
    std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}

int main()
{
    tesxxx();
    test1();
    test2();
    test3();
}

结果

tesxxx[16840]-x start...
tesxxx(non-multi-thread) ->  total_1: 5000000050000000 elapsed time(ms): 8231ms
tesxxx(non-multi-thread) ->  total_1: 5000000050000000 elapsed time(s): 8s
thread[16840]-1 start...
test1(mutex/multi-thread) ->  total_1: 10000000100000000 elapsed time(ms): 17387ms
test1(mutex/multi-thread) ->  total_1: 10000000100000000 elapsed time(s): 17s
thread[16840]-2 start...
test2(atomic/multi-thread) ->  total_2: 10000000100000000 elapsed time(ms): 31167ms
test2(atomic/multi-thread) ->  total_2: 10000000100000000 elapsed time(s): 31s
thread[16840]-3 start...
test3(CAS/multi-thread) ->  total_3: 10000000100000000 elapsed time(ms): 42721ms
test3(CAS/multi-thread) ->  total_3: 10000000100000000 elapsed time(s): 42s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneOnce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值