小编Jus*_*ber的帖子

使用std :: conditional_variable在条件上等待

为简单起见,我们假设我们只有一个条件变量来匹配由布尔值反映的单个条件.

1)为什么std::condition_variable::wait(...)在发送"通知"后才重新锁定互斥锁?

2)看到"1)"中的行为,这是否意味着当你这样做std::condition_variable::notify_all时只会使所有等待的线程被解锁/唤醒...但是按顺序而不是一次完成所有?如果是这样,可以做些什么来一次完成这一切?

3)如果我只关心线程睡眠直到满足条件并且不关心任何互斥锁获取,我该怎么办?是否有替代方案或当前的std::condition_variable::wait(...)方法是否应该被攻击?

如果要使用"hackery",这个函数是否可以解除条件中所有等待线程的阻塞,并且可以从任何(每个线程)线程调用它:

//declared somehwere and modified before sending "notify"(ies)
std::atomic<bool> global_shared_condition_atomic_bool;

//the single(for simplicity in our case) condition variable matched with the above boolean result
std::condition_variable global_shared_condition_variable;

static void MyClass:wait()
{
    std::mutex mutex;
    std::unique_lock<std::mutex> lock(mutex);

    while (!global_shared_condition_atomic_bool) global_shared_condition_variable.wait(lock);
}
Run Code Online (Sandbox Code Playgroud)

它会被随机的"等待"线程调用,如下所示:

void random_thread_run()
{
    while(someLoopControlValue)
    {
        //random code...
        MyClass:wait(); //wait for whatever condition the class+method is for.
        //more random code...
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

门类

#ifndef Gate_Header
#define Gate_Header …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading condition-variable blocking c++11

6
推荐指数
1
解决办法
7409
查看次数

BCrypt的主要C / C ++库是什么?

当前使用BCrypt的主要C / C ++库是什么?

OpenSSL真的不支持bcrypt吗?我还没有在它的加密库中找到任何东西。

encryption passwords openssl cryptography static-libraries

5
推荐指数
1
解决办法
2699
查看次数