简体   繁体   English

使用Mutex进行锁定和同步

[英]Locking and synchronization using Mutex

I am creating a program that will run the same function in several processes and several threads so I created a function to achieve locking and synchronization which is 我正在创建一个程序,它将在多个进程和多个线程中运行相同的功能,因此我创建了一个函数来实现锁定和同步,这是

HANDLE WaitOnMutex(char* mt)
{
    HANDLE ghMutex=NULL; 
    DWORD lastError=-1;
    do
    {
        ghMutex = CreateMutex(NULL,TRUE,mt);
        lastError=  GetLastError();
        if(lastError!=ERROR_SUCCESS)
        {
            CloseHandle(ghMutex);
            Sleep(2000);
        }
    }
    while(lastError!=ERROR_SUCCESS);
    return ghMutex;
}

and I am using it like the following 我正在使用它,如下所示

    HANDLE mtx=WaitOnMutex("Global\\DBG_MY_APP");
    //Do the work that needs sync
    CloseHandle(mtx)

Is this a proper way to lock this function ? 这是锁定此功能的正确方法吗? or do I need to use a different method.. 还是我需要使用其他方法。

Note: I am using "Global" because some parts of my app is winService and I need to lock between session-Isolated processes 注意:我使用“全局”是因为我的应用程序的某些部分是winService,我需要在会话隔离的进程之间锁定

The code is working in testing environment, but I am not sure if I am doing it the right way 该代码在测试环境中运行,但是我不确定是否以正确的方式进行操作

What you have coded is a busy wait, which is less than ideal under most circumstances. 您所编码的是一个忙碌的等待,在大多数情况下都不理想。 Not only that, but you've wrapped it in heavyweight mutex creation and release calls. 不仅如此,您还将其包装在重量级的互斥量创建和释放调用中。

To use a named mutex for cross-process synchronization, each process should call CreateMutex only once. 要使用命名的互斥锁进行跨进程同步,每个进程应仅调用一次CreateMutex You then keep the mutex handle around, and use WaitForSingleObject to wait for it and ReleaseMutex to release the lock. 然后,保持互斥锁句柄不变,并使用WaitForSingleObject等待它,然后使用ReleaseMutex释放锁。 You can then lock it again with WaitForSingleObject next time you need to access the protected resource, and so forth. 然后,下次需要访问受保护的资源时,可以使用WaitForSingleObject再次将其锁定,依此类推。

When your process is done with the mutex "forever" (eg because the process is exiting) then you call CloseHandle . 当使用“永远”互斥量完成进程时(例如,由于进程正在退出),则调用CloseHandle

I guess this will sort of work, for a sufficiently loose definition of "work". 我想对于“工作”的定义应该足够宽松,这样可以完成一些工作。 It's not how I'd do the job though. 不过这不是我的工作方式。

I think I'd do things more like this: 我想我会做更多这样的事情:

Somewhere in one-time initialization code: 一次性初始化代码中的某处:

HANDLE mutex = CreateMutex(name); // yes, there are more parameters here.

Then to write to the log: 然后写入日志:

WaitForSingleObject(mutex, INFINITE);

// write to log

ReleaseMutex(handle);

Then during one-time shut-down code: 然后在一次性关闭代码中:

CloseHandle(mutex);

If you're using C++ instead of C, you usually want to handle this via RAII though, so you'd create a log object that does the CreateMutex call in its ctor, and the CloseHandle call in it dtor. 如果您使用的是C ++而不是C,则通常希望通过RAII进行处理,因此您将创建一个日志对象,该对象在其ctor中执行CreateMutex调用,并在dtor中进行CloseHandle调用。 Then you'd have a write functor that does the WaitForSingleObject in its ctor, writes to the log in its operator() , and does the ReleaseMutex in its dtor. 然后,您将具有一个写函子,该函子在其ctor中执行WaitForSingleObject ,在其operator()写入日志,并在其dtor中执行ReleaseMutex This keeps most of your code relatively simple, and maintains exception safety with little or no extra work. 这样可以使您的大多数代码相对简单,并且只需很少甚至不需要额外的工作就可以维护异常安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM