C
C
Chemodan2282020-12-18 13:58:33
C++ / C#
Chemodan228, 2020-12-18 13:58:33

Question about using mutex and lock_guard?

The question is: Let's say we have a structure in which there is a lock_guard. We create a variable of the type of this structure (in the constructor we pass mutex to lock_guard). And it turns out that the code section (from the creation of the variable to the call of the destructor) will the code be executed by only one thread?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-12-18
@TopToster

And it turns out that the code section (from the creation of the variable to the call of the destructor) will the code be executed by only one thread?

As you write, so it will be fulfilled.
In my opinion, there is no reason to sew lock_guard into a structure/class, it should be in a function/method immediately created and deleted as quickly as possible. If this results in extra code, that's a small price to pay for thread safety.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question