As known,

The task of the mutex is to protect the object from access to it by other threads other than the one that has taken over the mutex. At any given time, only one thread can own an object protected by a mutex. If another thread needs access to a variable protected by a mutex, then this thread is blocked until the mutex is released.

But how does the mutex determine which particular resource / object / variable should be blocked? After all, no parameters are passed to him from which he could know what to protect.

If that, speech about std::mutex , with ++ 11 .

    5 answers 5

    I will add the answer @ alexis031182.

    A mutex or semaphore, as its generalization (specifically, an object (variable) located at a certain address) is a synchronization point for code streams of one or (in some cases) several processes.

    And nothing more. He does not protect anything. No objects in memory, no code sections. A thread that does not call a lock operation with this mutex can do what it wants with any variables and parts of the code. Protection is made at the logical level (in the head of the developer).

    In fact, the code stream that caused the lock operation for the specified mutex increments the counter associated with this mutex and continues execution if the counter was zero.

    Otherwise, this thread waits until the counter becomes 0 (then it in turn will increase it and continue to execute), which will happen when someone calls unlock , decreasing the counter by 1.

      In fact, no way. It does not protect a particular object.

      The mutex only ensures that the data accesses under the mutex are serialized. (That is, data races are not possible.) The responsibility of working with the necessary objects lies with you, the programmer.

      What you describe is not about what the mutex is doing , but only about what it is intended for and what you can do with it (yourself).

      • one
        "data race" = "race condition"? - Kromster
      • @Kromster: Yeah, but it’s the data race. Because there may still be other races. - VladD
      • @VladD you can win the race! - αλεχολυτ

      Any mutex has lock and unlock functions (they can be called differently, but the essence does not change. Some mutexes can use raii, automatically calling the necessary functions).

      And it protects a piece of code that cannot be executed if the mutex is in a locked state. That is, one mutex can protect many different places and they will not be executed at the same time (relevant for multithreaded execution). On the other hand, there can be many mutexes in the system at the same time and they will not interfere with each other.

      • 2
        не будут мешать друг-дружке ... until one day there is a dead-lock :) - αλεχολυτ
      • This is only if someone tries to block them in an arbitrary mode. Sorting partially solves this problem. - KoVadim

      A mutex essentially provides what is called a "critical section" (critical section) - i.e. code that can be executed at a time by only one thread.

      But if we hang a given mutex on a code that refers to a variable, then it seems that it protects an object, and not a code fragment.

      • corrected for a more common version of naming in Russian. - ixSci
      • @ixSci Funny where exactly is more common? Search in Google "critical section C ++" gives 3480 results, and the section - 45800. How to put it ... Here, forgive the pun, uncritically, but in the Russian-language literature it is the critical section of the code that is accepted ... - Harry
      • Unfortunately, I have no technical books left in Russian, so I cannot show where exactly. As an example, I can cite microsoft and the fact that I first hear the phrase “critical section”, in relation to the critical section concept from programming, and I have a higher specialized education. - ixSci
      • 3
        Yes, and Wikipedia, too, for my version. In general, as I said, in Russian there is the accepted name “critical section” - “critical section”. - ixSci
      • Well, what I found at hand was Kernigan and Donovan's Go Programming Language, Meyers ей Efficient and Modern C ++, Josattis Стандартная Standard C ++ Library везде everywhere section. I don’t want to argue, I’m just explaining why, why I’m writing the “section”. - Harry

      Let the allegory ... Whoever stood up, that and sneakers. There is no need to note this fact on themselves. First put on - own, you won. Accordingly, who did not have time, he was late, and will remain in a dreary wait until such a useful, but completely inseparable resource is released. Of course, to some extent the example will be dragged down by the ears, since in real life it is impossible to exclude the option of resolving the issue by force. But we are talking about mutexes, and therefore the unconditional control over the process of redistribution of property goes without saying. Only the goodwill of the lucky one, this time having done it first, will allow another to take advantage of the object of desired comfort.

      I will add myself.

      Allegory did not work out with slippers because many people can imagine working life without them. And the essence is not in slippers, but in the context of implementation. Through a mutex, the execution context of one thread blocks / releases the execution context of another or the other. And so - yes, slippers have nothing to do with it, it's in my head.

      - By the way, d'Artagnan: I heard that a certain Milon of Croton was doing amazing things. For example, he wrapped his head with a rope and with one movement of the head muscles !!! ... ripped it. He knocked down the bull with one blow of his fist and carried him off on his shoulders ... well, and the like. When I found out about all this ... I did it at Pierrefon ... well, everything is the same as this Milo. Except for one thing: I could not break my head with a rope.

      “This is because you have power ... not in your head.”