Confusion on CreateMutex

Let it say that I call CreateMutex.

HANDLE h;
h=CreateMutex(NULL, TRUE, NULL);
waitforsingleobject(h, INFINITE);
////Random Code
ReleaseMutex(h);

Assuming I have multiple threads, does the first thread to access the createmutex function essentially block all other threads from the random code section until the mutex release is called correctly?

+3
source share
4 answers

This is not so because you created an unnamed mutex (the third parameter is the name). Assuming that the sample code is executed in several threads, each thread will create a new unnamed mutex and immediately gain access to the critical section (Random Code), because they are only waiting for their mutex.

, h , , CreateMutex CreateMutex ( ). CreateMutex .

+13

, , .

.

+1

, , , . , .

, mutex , , . , , .

, , WaitForSingleObject .

, , ownerip term terminated, .

, ReleaseMutex mutex, ownerip. , RAII ( , return ReleaseMutex, ).

-, , , CloseHandle MutexHandle.

, , . : " Mutex"

. :

+1

Only if the same mutex reference is used for all threads / processes. You do not share it, you create it every time. You will need to name it so that they all get the same mutex so that your code works.

0
source

Source: https://habr.com/ru/post/1794667/


All Articles