6 Comments
User's avatar
Vadim Mi's avatar

That's great that compiler helps avoid mistakes.

I believe ordering locks seems to be simpler in C++ https://en.cppreference.com/w/cpp/thread/scoped_lock

Expand full comment
Art's avatar

It is good to know that both std::lock and std::scoped_lock are using a deadlock avoidance algorithm and the order of passed mutexes is irrelevant.

Am I right to assume that in case of many mutexes lock/scoped_lock does not provide a way to have fine grained mutex control? They all will be released in the same time, correct?

Expand full comment
Vadim Mi's avatar

Lock/scoped_lock holds mutexes in the constructor and releases the mutexes in the destructor.

Do you want to release only some of the mutexes while keeping the rest locked?

Could you help me understand use case in which you want to do that?

Expand full comment
Art's avatar

That's right, I want to allow some of the mutexes to be released as soon as possible

Expand full comment
Vadim Mi's avatar

I see, then I'd suggest using unique_lock + lock. See the example: https://godbolt.org/z/97d7oq8Wz

Look at 27 line.

Expand full comment
Art's avatar

Thanks a lot Vadim, I learned something new today!

Expand full comment