C++ 自旋锁的替代屏障?

C++ 自旋锁的替代屏障?,c++,multithreading,C++,Multithreading,假设我有一个函数,多个线程需要在一个锁步骤中运行 std::atomic<bool> go = false; void func() { while (!go.load()) {} //sync barrier ... } std::atomic go=false; void func(){ 而(!go.load()){}//sync barrier ... } 我想摆脱自旋锁,用基于互斥锁的东西替换它,因为我有很多线程在做各种事情,自旋锁十

假设我有一个函数,多个线程需要在一个锁步骤中运行

std::atomic<bool> go = false;  

void func() {
        while (!go.load()) {} //sync barrier
        ...
}
std::atomic go=false;
void func(){
而(!go.load()){}//sync barrier
...
}
我想摆脱自旋锁,用基于互斥锁的东西替换它,因为我有很多线程在做各种事情,自旋锁十几个线程对整体吞吐量是有害的,例如,如果我在自旋锁中包含Sleep(1),它运行得更快


例如,STL中是否有类似于HLSL中AllMemoryBarrierWithGroupSync()的内容?基本上,它只会让每个线程在屏障处休眠,直到所有线程都达到它。

如果您愿意使用实验功能,则或将帮助您。否则,您可能会使用或使用
shared\u lock
(C++17功能)创建自己的类似构造

使用
shared_mutex
实现屏障:

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <vector>

std::shared_mutex mtx;
std::condition_variable_any cv;
bool ready = false;

void thread_func()
{
    {
        std::shared_lock<std::shared_mutex> lock(mtx);
        cv.wait(lock, []{return ready;});
    }
    std::cout << '0';
    //Rest of calculations
}


int main()
{
    std::vector<std::thread> threads;
    for(int i = 0; i < 5; ++i)
        threads.emplace_back(thread_func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
        std::unique_lock<std::shared_mutex> lock(mtx);
        std::cout << "Go\n";
        ready = true;
    }
    cv.notify_all();
    for(auto& t: threads)
        t.join();
    std::cout << "\nFinished\n";
}
#包括
#包括
#包括
#包括
#包括
#包括
std::共享互斥mtx;
std::条件变量任何cv;
bool ready=false;
无效线程_func()
{
{
std::共享锁(mtx);
wait(lock,[{return ready;});
}

std::cout如果您愿意使用实验特性,那么或将帮助您。否则,您可能会使用或使用
共享锁(C++17特性)创建自己的类似构造

使用
shared_mutex
实现屏障:

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <vector>

std::shared_mutex mtx;
std::condition_variable_any cv;
bool ready = false;

void thread_func()
{
    {
        std::shared_lock<std::shared_mutex> lock(mtx);
        cv.wait(lock, []{return ready;});
    }
    std::cout << '0';
    //Rest of calculations
}


int main()
{
    std::vector<std::thread> threads;
    for(int i = 0; i < 5; ++i)
        threads.emplace_back(thread_func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
        std::unique_lock<std::shared_mutex> lock(mtx);
        std::cout << "Go\n";
        ready = true;
    }
    cv.notify_all();
    for(auto& t: threads)
        t.join();
    std::cout << "\nFinished\n";
}
#包括
#包括
#包括
#包括
#包括
#包括
std::共享互斥mtx;
std::条件变量任何cv;
bool ready=false;
无效线程_func()
{
{
std::共享锁(mtx);
wait(lock,[{return ready;});
}

std::cout听起来您想要做的正是条件变量的好处

bool go = false;
std::mutex mtx;
std::condition_variable cv;

void thread_func()
{
    {
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, []{ return go; });
    }
    // Do stuff
}

void start_all()
{
    {
        std::unique_lock<std::mutex> lock(mtx);
        go = true;
    }
    cv.notify_all();
}
bool-go=false;
std::互斥mtx;
std::条件变量cv;
无效线程_func()
{
{
std::唯一锁(mtx);
cv.wait(lock,[{return go;});
}
//做事
}
void start_all()
{
{
std::唯一锁(mtx);
去=真;
}
cv.通知所有人();
}

听起来您想要做的正是条件变量的好处

bool go = false;
std::mutex mtx;
std::condition_variable cv;

void thread_func()
{
    {
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, []{ return go; });
    }
    // Do stuff
}

void start_all()
{
    {
        std::unique_lock<std::mutex> lock(mtx);
        go = true;
    }
    cv.notify_all();
}
bool-go=false;
std::互斥mtx;
std::条件变量cv;
无效线程_func()
{
{
std::唯一锁(mtx);
cv.wait(lock,[{return go;});
}
//做事
}
void start_all()
{
{
std::唯一锁(mtx);
去=真;
}
cv.通知所有人();
}

值得的是,
while(!go.load())
可以写
while(!go)
。值得的是,
while(!go.load())
可以写
while(!go)