2017년 6월 27일 화요일

락 관련 속도 비교

1. atomic과 mutex 차이
#include <atomic>
#include <iostream>
#include <ppl.h>
#include <Windows.h>

#pragma comment(lib, "Winmm.lib")

    std::atomic<int> bIsWrite = 0;
    //int b = 1;
    //lock
    auto time = timeGetTime();
    Concurrency::parallel_for(0, 100000, [&](int i) {
        i;
        int a = 0;
        if (bIsWrite.compare_exchange_weak(a, 1))
        {
            std::cout << std::endl;
        }
    });
    std::cout << timeGetTime() - time << std::endl;
    //thread

    std::recursive_mutex m_kMutex;
    bool bRead = false;
    time = timeGetTime();
    Concurrency::parallel_for(0, 100000, [&](int i) {
        i;
        std::lock_guard<std::recursive_mutex>kLock(m_kMutex);
        if (bRead == false)
        {
            bRead = true;
            std::cout << std::endl;
        }
    });
    std::cout << timeGetTime() - time << std::endl;

결과
15
50