dimanche 26 janvier 2020

Why c++11 std::lock and std::scoped_lock need at least 2 parameters?

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.

Aucun commentaire:

Enregistrer un commentaire