参考MSDN例子
First Process
#include <windows.h>
#include <stdio.h>
#include <conio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("NameOfMutexObject")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %dn", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutexn");
else{
while (1)
{
WaitForSingleObject(hMutex, INFINITE);
printf("CreateMutex created a new mutex.n");
ReleaseMutex(hMutex);
Sleep(1000);
}
}
// Keep this process around until the second process is run
_getch();
CloseHandle(hMutex);
return 0;
}
Second Process
#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("NameOfMutexObject")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %dn", GetLastError() );
else{
while (1)
{
printf("wait for single objectn");
WaitForSingleObject(hMutex, INFINITE);
printf("OpenMutex successfully opened the mutex.n");
ReleaseMutex(hMutex);
Sleep(1000);
}
}
CloseHandle(hMutex);
return 0;
}
断点停在任何一个程序WaitForSingleObject和ReleaseMutex程序中间,另外一个程序将等待
最后
以上就是忧郁夏天最近收集整理的关于vc进程间互斥Mutex的全部内容,更多相关vc进程间互斥Mutex内容请搜索靠谱客的其他文章。
发表评论 取消回复