What happens if I write a few mutex locks? Will both mutex be selected on the same topic?
A thread can simultaneously hold any number of mutexes. In your code example, a thread entering an area protected by Mutex1 will try to get Mutex2 , waiting for another thread to exit first if necessary. In the code you present, there is no particular reason to think that it will fail.
I also read that multiple mutex locks can be deadlocked. Can someone explain to me how I can cause a deadlock blocking part of the code with 2 mutexes?
Suppose there are two threads, one of which contains mutex1, and the other contains mutex2 (in this case, at least the latter should run something different from your pseudo-code). Now suppose that each thread is trying to get a different mutex, not letting go of the one that it already has. The first thread must receive mutex2 in order to continue, and cannot do this until the other releases it. The second thread must get mutex1 to continue, and cannot do this until the other releases it. Ergo, not a single thread can go on - this is a dead end.
The general rule is the following: if there is a set of mutexes, all of which two or more threads may each of them want to conduct at the same time, then there must be a fixed relative ordering of these mutexes, which each thread performs when receiving any subset of them.
source share