0

我已经在互联网上搜索了一段时间,以找到关于不涉及单例模式的 DCL 的讨论。所以相反,我只想问我想知道什么。

DCL在这里合适吗?

std::shared_ptr<bool> mybool_smart_ptr(new bool); // initialized to false in main thread
std::shared_ptr<std::mutex> mtx(new std::mutex);
std::shared_ptr<std::condition_variable_any> cond(new std::condition_variable_any);


void proceed_to_work() {
    if(*mybool_smart_ptr)
         return;

    std::unique_lock<std::mutex> lock(*mtx);
    // if DCL is broken, would a fence here help?
    if(*mybool_smart_ptr)
         return;
    cond->wait(lock);
}

任何作家*mybool_smart_ptr都这样做是无锁的。产生的线程所做的工作是全部或全部,这就是它们所等待的。

4

0 回答 0