栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ mutex, atomic, CAS性能

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ mutex, atomic, CAS性能

#include 
#include 
#include 
#include 
#include 
#include 
#include 



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 elapsed_seconds = end - start;
    std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast(elapsed_seconds).count() << "msn";
    std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast(elapsed_seconds).count() << "sn";
}

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 elapsed_seconds = end - start;
    std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(ms): "   << std::chrono::duration_cast(elapsed_seconds).count() << "msn";
    std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(s): "   << std::chrono::duration_cast(elapsed_seconds).count() << "sn";
}

std::atomic 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 elapsed_seconds = end - start;
    std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(ms): " << std::chrono::duration_cast(elapsed_seconds).count() << "msn";
    std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(s): " << std::chrono::duration_cast(elapsed_seconds).count() << "sn";
}

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 elapsed_seconds = end - start;
    std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast(elapsed_seconds).count() << "msn";
    std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast(elapsed_seconds).count() << "sn";
}

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

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号