当前位置: 代码迷 >> WinCE >> wince6 互斥Mutex的用法有关问题
  详细解决方案

wince6 互斥Mutex的用法有关问题

热度:99   发布时间:2016-04-28 12:48:08.0
wince6 互斥Mutex的用法问题
在进程1中:
static HANDLE g_VK3224_MUTEX = NULL;
g_VK3224_MUTEX = CreateMutex(NULL, FALSE, _T("VK3224_MUTEX"));
WaitForSingleObject(g_VK3224_MUTEX, INFINITE);
....
....
ReleaseMutex(g_VK3224_MUTEX)

在进程2中:
static HANDLE g_VK3224_MUTEX = NULL;
g_VK3224_MUTEX = CreateMutex(NULL, FALSE, _T("VK3224_MUTEX")); 

//GetLastError() == ERROR_ALREADY_EXISTS 在这里调用GetLastError是等于ERROR_ALREADY_EXISTS的,两g_VK3224_MUTEX值不一样

WaitForSingleObject(g_VK3224_MUTEX, INFINITE);
....
....
ReleaseMutex(g_VK3224_MUTEX)

现在起不到互斥的效果,进程1进入...但还没调用ReleaseMutex,进程2也能进入...里面,我上面的用法有问题吗?高手们帮忙指点下,多谢拉!


CreateMutex返回值说明:
Return Value
A handle to the mutex object indicates success. If the named mutex object existed before the function call, the function returns a handle to the existing object, and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.

NULL indicates failure. To get extended error information, call GetLastError.




------解决方案--------------------
一个进程使用:CreateMutex,第二个使用:OpenMutex,而不是使用:CreateMutex
------解决方案--------------------
可以参考一下这篇文章http://blog.csdn.net/brantyou/article/details/8063107
------解决方案--------------------
我帮你验证了下,Windows和WinCE下是可以的。你是在驱动里面用的么?
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, cmd_t lpCmdLine, int nShowCmd)
{
static HANDLE g_VK3224_MUTEX = NULL;
g_VK3224_MUTEX = CreateMutex(NULL, FALSE, _T("VK3224_MUTEX"));
WaitForSingleObject(g_VK3224_MUTEX, INFINITE);
::MessageBox(NULL, _T("Test!"), _T("tip"), IDOK);
ReleaseMutex(g_VK3224_MUTEX);
return 0;
}
  相关解决方案