Mutex
-
In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance
-
In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance
LCI wrote:
I have 4 threads that call a method in a .dll
If you want only intraprocess synchronisation, IMHO then go for CriticalSection, and don't bother about the Mutex questions.
-
LCI wrote:
I have 4 threads that call a method in a .dll
If you want only intraprocess synchronisation, IMHO then go for CriticalSection, and don't bother about the Mutex questions.
I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??
-
I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??
OpenMutex get the mutex Handle for an existing mutex. You must have a Mutex Created with CreateMutex. what about Critical section?
-
I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??
I mean by critical section, the synchronisation object criticalsection Using Critical Section Objects[^]
-
In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance
LCI wrote:
1. Do i use CreateMutex or OpenMutex
In the thread initializations, use CreateMutex(). If the mutex already exists then you'll get a handle to it and GetLastError() returns ERROR_ALREADY_EXISTS. If it's a new mutex you'll still get a handle to it. As others have mentioned, you don't need a named mutex if your threads are all in the same process - which includes your DLL.
LCI wrote:
2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter.
Windows security is beyond the scope of this board, but there is documentation[^] Pass NULL if in doubt.
LCI wrote:
. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID?
You can use a GUID (converted to a character string). Any unique string. But again, no need for a named mutex if you only have one process - you can use a critical section instead. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: