のドキュメントを読んstd::mutex::try_lock
でいたところ、次の例がありました。
#include <iostream>
#include <mutex>
int main()
{
std::mutex test;
if (test.try_lock() == true)
std::cout << "lock acquired" << std::endl;
else
std::cout << "lock not acquired" << std::endl;
test.unlock(); // now unlock the mutex
test.lock(); // to lock it again
if (test.try_lock()) // true can be left out
std::cout << "lock acquired" << std::endl;
else
std::cout << "lock not acquired" << std::endl;
test.lock(); // and now the finale (a block)
}
この 2 番目の if ステートメントで、彼は true を省略できると言っています。なぜこれは2番目のものではそうですが、最初のものではそうではありません. 私がチェックしたところtry_lock
、ブール値を返すと表示されているので、どうして真でも偽でもないので、== true
チェックが不要になるのでしょうか?