概述
参考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所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复