首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > WinCE >

wince6 互斥Mutex的用法有关问题

2012-11-09 
wince6 互斥Mutex的用法问题在进程1中:static HANDLE g_VK3224_MUTEX NULLg_VK3224_MUTEX CreateMute

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;
}

热点排行