我可以使用std :: atomic而不是std :: mutex在C++中跨线程共享变量吗?

Pet*_*rál 6 c++ multithreading stdatomic

我制作了一个计算多核上素数的程序.(请忽略这样一个事实,即算法不是完全有效,数字0和1在这里被认为是素数.目的只是练习使用线程.)

该变量taken(下一个要测试的数字)在8个线程中共享.

问题是它可以通过一个线程递增,然后由另一个线程递增,当它已经增加两次(或更多次)时由它们读取,因此可以跳过某些值,这是一件坏事.

我认为它可以通过使用std::atomic_uint变量类型来解决,但我显然是错的.

有没有什么方法可以解决这个问题,而不需要使用,std::mutex因为我听说它会导致相当大的开销?源代码:

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

int main()
{
    const uint MAX = 1000;

    std::vector<bool> isPrime(MAX), done(MAX);
    std::fill(done.begin(), done.end(), false);
    std::atomic_uint taken{0}; //shared variable
    std::vector<std::thread> threads;
    auto start = std::chrono::system_clock::now();

    for (uint i = 0; i < 8; ++i) {
        threads.emplace_back(
            [&](){
                bool res;
                for (uint tested; (tested = taken.fetch_add(1)) < MAX; ) { //taken should be incremented and copied atomically
                    res = true;
                    for (uint k = 2; k < tested; ++k) {
                        if (tested % k == 0) {
                            res = false;
                            break;
                        }
                    }
                    isPrime[tested] = res;
                    done[tested] = true;
                }
            }
        );
    }
    for (auto & t : threads) {
        t.join();
    }

    auto end = std::chrono::system_clock::now();
    auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    uint num = std::count_if(isPrime.begin(), isPrime.end(), [](bool b){return b;});
    uint nDone = std::count_if(done.begin(), done.end(), [](bool b){return !b;});
    std::cout << "number: " << num << " duration: " << milliseconds.count() << '\n';
    std::cout << "not done: " << nDone << '\n';
    for (uint i = 0; i < MAX; ++i) { //Some numbers are always skipped
        if (!done[i]) {
            std::cout << i << ", ";
        }
    }
    std::cout << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码是使用g++with -O3-pthreadarguments 编译的.输出:

number: 169 duration: 1
not done: 23
143, 156, 204, 206, 207, 327, 328, 332, 334, 392, 393, 396, 502, 637, 639, 671, 714, 716, 849, 934, 935, 968, 969,
Run Code Online (Sandbox Code Playgroud)

每次输出都不同.

Dan*_*ica 6

专门化std::vector<bool>将值压缩为单个位.因此,在单个字节中存在多个向量元素,即,在单个存储器位置中.因此,您的线程更新相同的内存位置而不进行同步,这是一个数据争用(因此根据标准,未定义的行为).

尝试std::vector<bool>改为std::vector<char>.