我正在阅读文档,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)
}
Run Code Online (Sandbox Code Playgroud)
在第二个if语句中,他说真的可以被排除在外.为什么这是第二个,而不是第一个.我检查过它说try_lock
返回一个布尔值,那么它怎么能不是真或假,从而使== true
检查变得多余?
归档时间: |
|
查看次数: |
133 次 |
最近记录: |