Just found that these 2 utils needs at least 2 parameters, e.g. 2 mutex to lock.
Needs to be like this(from cppreference.com):
void assign_lunch_partner(Employee &e1, Employee &e2)
{
static std::mutex io_mutex;
{
std::lock_guard<std::mutex> lk(io_mutex);
std::cout << e1.id << " and " << e2.id << " are waiting for locks" << std::endl;
}
{
std::scoped_lock lock(e1.m, e2.m);
}
}
Does it make any sence to require at lease 2 params? What was the design consideration, wish to know more details.
Thanks a lot.
It doesn't require two, it can lock one or more.
From the cppreference page you took your example from (emphasis mine):
std::scoped_lockis a convenience utility for acquiring multiple mutexes - it will use deadlock avoiding mechanism under the hood. In C++11 and C++14 we only hadstd::lock(), but it is not a RAII mechanism (it will not unlock mutexes automatically).You can also use
std::scoped_lockwith single mutex, then it becomes equivalent tostd::lock_guard