可変引数版try_lock_until()/try_lock_for()

Why no timeout support in std::lock?


C++11の標準ライブラリでは、階層的なロック取得をサポートするために、非メンバ関数の可変引数関数テンプレートバージョンとして、std::lock()とstd::try_lock()が提供されています。
しかし、標準のミューテックスライブラリではロック戦略として、lock()、try_lock()のほかに、タイムアウト時間を指定するtry_lock_for()/try_lock_until()というのが、std::timed_mutex、std::recursive_timed_mutexクラスでサポートされています。では、これらの操作にはなぜ可変引数バージョンが存在しないのか、というのが上記StackOverflowでの質問です。


それに対して、Boost.Threadライブラリの作者であり、C++11標準のスレッドライブラリ作者でもあるAnthony Williamsから説明がありました:

std::timed_mutex has try_lock_until and try_lock_for member functions. However, you are right that there is no equivalent of std::lock with a timeout.

Locking a mutex with a timeout is only of use in particular niches. Locking multiple mutexes with a timeout was not something anyone felt strongly enough about to propose, so it isn't in C++11.

The standards committee is currently actively seeking proposals for the next standard. If you feel that a timeout-aware equivalent of std::lock is valuable, why not write a proposal?

std::timed_mutexは、try_lock_untilとtry_lock_forメンバ関数を持っています。しかしstd::lockにタイムアウト相当の機能は存在しないので、あなたの疑問は正しいです。

ミューテックスでは、タイムアウトのロック操作だけはとくにニッチです。複数ミューテックスに対するタイムアウトロックは、強く提案するのに十分だと感じなかったため、C++11にはありません。

標準化委員会は現在、次の標準のための提案を積極的に求めています。std::lockのタイムアウト機能に価値があると感じるなら、提案を書いてみませんか?

だそうです。とくに必要だと感じなかったからないだけで、十分な理由があって提案すれば入るかもしれません。


参照:
std::lock() - cpprefjp
std::try_lock() - cpprefjp