Ich habe eine Weile gesucht und ich habe mich entschieden zu fragen, wie ich nicht die richtige Antwort finden kann.Verwendung von Mutexe in Interporz Sync in c
Eigentlich muss ich zwei Prozesse
Verfahren 1:
#include <windows.h>
#include <stdio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
TRUE, // mutex owned
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError());
else
if (GetLastError() == ERROR_ALREADY_EXISTS)
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n");
if(WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
printf("Error while waiting for the mutex.\n");
else
printf("Mutex openned by second process.\n");
CloseHandle(hMutex);
return 0;
}
Prozess 2:
#include <windows.h>
#include <stdio.h>
// This process opens a handle to a mutex created by another process.
int main(void)
{
HANDLE hMutex;
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %d\n", GetLastError());
else printf("OpenMutex successfully opened the mutex.\n");
if(!ReleaseMutex(hMutex)){
printf("Error while releasing the mutex.\n")
}
CloseHandle(hMutex);
return 0;
}
Wenn ich also den ersten Prozess führen Sie es einfach nicht für die warten zweiter Prozess, um den Mutex freizugeben, aber; Da der Mutex nicht synchronisiert ist, sollte er nicht warten, bis ein Prozess/Thread ihn freigibt und die Nachricht dann ausdruckt?
Im ersten Prozess warten Sie einen Mutex Sie bereits besitzen, so dass die Wartezeit zufrieden sein werden sofort verwendet werden soll; Im zweiten Prozess geben Sie einen Mutex frei, den Sie * nicht besitzen, was illegal ist. Ich denke, Eugene hat recht, du solltest stattdessen ein Ereignis verwenden. –