c++ - Why is an explicit comparison needed when using try_lock()? -
i reading documentation of std::mutex::try_lock
, had example on it:
#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(); // unlock mutex test.lock(); // lock again if (test.try_lock()) // true can left out std::cout << "lock acquired" << std::endl; else std::cout << "lock not acquired" << std::endl; test.lock(); // , finale (a block) }
in second if statement says true can left out. why second 1 not first. checked , says try_lock
returns boolean, how can not either true or false, making == true
check superfluous?
x == true
silly when x
has type bool
. test.try_lock()
returns bool
, in both cases test isn't needed.
Comments
Post a Comment