#include <mutex>
#include <iostream>
using namespace std;
struct LoudMutex : std::mutex {
using std::mutex::mutex;
void lock() { cout << "Locking!" << endl; this->mutex::lock(); }
bool try_lock() { cout << "Trying to lock: " << endl;
bool success = this->mutex::try_lock();
cout << success << endl;
return success; }
void unlock() { cout << "Unlocking!" << endl; this->mutex::unlock(); }
};
int main() {
LoudMutex m;
{
cout << endl << "Case 1" << endl;
std::unique_lock<LoudMutex> l(m);
cout << "Hello!" << endl;
}
{
cout << endl << "Case 2" << endl;
auto l = std::unique_lock<LoudMutex>(m);
cout << "Hello!" << endl;
}
{
cout << endl << "Case 3" << endl;
// OOPS! this declares a function `l`, but doesn't invoke any methods!
auto l(std::unique_lock<LoudMutex>(m));
cout << "Hello!" << endl;
}
return 0;
}
I2luY2x1ZGUgPG11dGV4PgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTG91ZE11dGV4IDogc3RkOjptdXRleCB7Cgl1c2luZyBzdGQ6Om11dGV4OjptdXRleDsKCXZvaWQgbG9jaygpIHsgY291dCA8PCAiTG9ja2luZyEiIDw8IGVuZGw7IHRoaXMtPm11dGV4Ojpsb2NrKCk7IH0KCglib29sIHRyeV9sb2NrKCkgeyBjb3V0IDw8ICJUcnlpbmcgdG8gbG9jazogIiA8PCBlbmRsOwoJCQkJCSAgYm9vbCBzdWNjZXNzID0gdGhpcy0+bXV0ZXg6OnRyeV9sb2NrKCk7CgkJCQkJICBjb3V0IDw8IHN1Y2Nlc3MgPDwgZW5kbDsKCQkJCQkgIHJldHVybiBzdWNjZXNzOyB9CgoJdm9pZCB1bmxvY2soKSB7IGNvdXQgPDwgIlVubG9ja2luZyEiIDw8IGVuZGw7IHRoaXMtPm11dGV4Ojp1bmxvY2soKTsgfQp9OwoKaW50IG1haW4oKSB7CglMb3VkTXV0ZXggbTsKCQoJewoJCWNvdXQgPDwgZW5kbCA8PCAiQ2FzZSAxIiA8PCBlbmRsOwoJCX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