windows 之互斥量----mutex

CreateMutexA

CreateMutexExA

CreateMutexExW

CreateMutexW

 

可以用互斥量作为单实例应用控制,

如:

进程A创建互斥量Mutex,

启动进程B创建互斥量Mutex失败,这时候就不打开进程B而是打开进程A

 

 

HANDLE hMutex = ::CreateMutexW(NULL, FALSE, ptr);
if (hMutex == NULL)
{
  std::cout << "create mutex error with." << GetLastError() << std::endl;
}
else {
  std::cout << "create mutex successful." << std::endl;
}
if (ERROR_ALREADY_EXISTS == GetLastError())
{
  cout << "mutex has exists" << endl;
}
else
{
  cout << "create new muetex" << endl;
}

//记得创建完后释放互斥量.

CloseHandle(hMutex);

 

windows 之互斥量----mutex

上一篇:couchDB入门


下一篇:oracle 左右链接