C++11/std::atomic - 原子變量(不加鎖實現線程互斥)

1 原子操作

原子操作:一個獨立不可分割的操作。多線程編程需要保證線程安全,而線程安全一個很重要的特性就是原子性,即在同一時刻只有一個線程對原子進行操作,保證數據訪問的互斥性。

2 C++11原子變量

C++11提供了原子類型std::atomic,可以使用任意的類型作爲模板參數。在多線程中如果使用了原子變量,其本身就保證了數據訪問的互斥性,所以不需要使用互斥量來保護該變量了。

3 使用原子變量

3.1 沒有使用線程互斥的數據操作

#include <iostream>             
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>

long long globalCount = 0;

void ThreadFunction()
{
    for (int i=0;i<100000;++i)
    {
        globalCount += 1;
    }
}

int main()
{
    std::vector<std::thread> threads;

    std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();

    for (int i = 0; i < 10; ++i)
    {
        threads.push_back(std::thread(ThreadFunction));
    }

    for (int i=0;i<10;++i)
    {
        threads[i].join();
    }
    std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();

    std::cout << "當前總數爲:" << globalCount << std::endl;
    std::cout << "消耗時間爲:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
    getchar();

    return 0;
}

在這裏插入圖片描述

3.2 使用互斥量保證線程互斥

#include <iostream>             
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>

long long globalCount = 0;
std::mutex globalMutex;

void ThreadFunction()
{
    std::lock_guard<std::mutex> lock(globalMutex);
    for (int i=0;i<100000;++i)
    {
        globalCount += 1;
    }
}

int main()
{
    std::vector<std::thread> threads;

    std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();

    for (int i = 0; i < 10; ++i)
    {
        threads.push_back(std::thread(ThreadFunction));
    }

    for (int i=0;i<10;++i)
    {
        threads[i].join();
    }
    std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();

    std::cout << "當前總數爲:" << globalCount << std::endl;
    std::cout << "消耗時間爲:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
    getchar();

    return 0;
}

在這裏插入圖片描述

3.3 使用原子量std::atomic保證數據互斥

#include <iostream>             
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>

std::atomic<long> globalCount = 0;

void ThreadFunction()
{
    for (int i=0;i<100000;++i)
    {
        globalCount += 1;
    }
}

int main()
{
    std::vector<std::thread> threads;

    std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();

    for (int i = 0; i < 10; ++i)
    {
        threads.push_back(std::thread(ThreadFunction));
    }

    for (int i=0;i<10;++i)
    {
        threads[i].join();
    }
    std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();

    std::cout << "當前總數爲:" << globalCount << std::endl;
    std::cout << "消耗時間爲:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
    getchar();

    return 0;
}

在這裏插入圖片描述

有興趣可以訪問我的個站:www.stubbornhuang.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章