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