1,221
社区成员
发帖
与我相关
我的任务
分享
g_hMutex = CreateMutex(NULL, FALSE, NULL);
//DLL导出的函数
int__stdcall DllFun(BYTE *pBuf)
{
//有时候超时,有时候正常, WHY???
if (WaitForSingleObject(g_hMutex, 3000) == WAIT_OBJECT_0)
{
//memcpy(pBuf, g_Buf, dwSize);
while (ReleaseMutex(g_hMutex));
return 1;
}
return 0;
}
//线程函数
unsigned __stdcall ThreadFun(void *parm)
{
while(!g_bTermiate)
{
if (WaitForSingleObject(g_hMutex, 3000) != WAIT_OBJECT_0)
{
Sleep(2);
continue;
};
//对g_Buf进行操作, 不超过0.5秒
//.......
while (ReleaseMutex(g_hMutex));
}
return 0;
}
g_hMutex = CreateMutex(NULL, FALSE, NULL);
//DLL导出的函数
int__stdcall DllFun(BYTE *pBuf)
{
if (WaitForSingleObject(g_hMutex, 3000) == WAIT_OBJECT_0)
{
//memcpy(pBuf, g_Buf, dwSize);
ReleaseMutex(g_hMutex);
return 1;
}
return 0;
}
//以上有可能会超时
//////////////
int__stdcall DllFun(BYTE *pBuf)
{
int i=0;
while(i<3)//等待一定时间,也可以无限等待
{
if (WaitForSingleObject(g_hMutex, 3000) == WAIT_OBJECT_0)
{
//memcpy(pBuf, g_Buf, dwSize);
ReleaseMutex(g_hMutex);
return 1;
}
i++;
}
return 0;
}
/////////////
//线程函数
unsigned __stdcall ThreadFun(void *parm)
{
while(!g_bTermiate)
{
if (WaitForSingleObject(g_hMutex, 3000) != WAIT_OBJECT_0)
continue;
//对g_Buf进行操作, 不超过0.5秒
ReleaseMutex(g_hMutex);
}
return 0;
}