开发者

Have I Implemented Mutal Exchange Correctly?

I'm trying to implement mutual exchange on a multi-threaded program. I'm not sure if I'm doing it right, and it's difficult to test.

In main.cpp, I have something like this:

//mutex handle
HANDLE hIOMutex = CreateMutex (NULL,FALSE,NULL);

//main
while (1) {
    Sleep(STEP);

    WaitForSingleObject(hIOMutex,INFINITE);

    //DO STUFF

    ReleaseMutex(hIOMutex);
}
return 0;

And in a function used by the other thread:

void Case::account_login(Connection* Con) {
    //mutex handle
    HANDLE hIOMutex = CreateMutex (NULL,FALSE,NULL);

    WaitForSingleObject(hIOMutex,INFINITE);

    //DO STUFF

    ReleaseMutex(hIOMutex);
    return;
}

Is this correct? Are the sections of code even using the same HANDLE or am I declaring them locally and therefore screwing up the functionality?

If I've missed out any vital information, let me know.

Th开发者_StackOverflow社区anks.


Are the sections of code even using the same HANDLE or am I declaring them locally and therefore screwing up the functionality?

No, they are not using the same handle or the same mutex, for that matter (there may be multiple handles to the same mutex). In the posted code, the two threads each have their own mutex, which means access to common data protected by these two mutexes will not be "mutually exclusive".

Your options are more or less:

  1. pass the mutex as a parameter to the thread function
  2. define the mutex handle as a member of the Connection struct/class
  3. use a named mutex (create the named mutex on one side and open the named mutex on the other side)
  4. make the handle a global variable.

Edit: I put "make the handle a global variable" last for a good reason: it's the worst choice in the group. However, since OP insisted on an example...

// globals.h
#include <windows.h>
extern HANDLE hIOMutex;

// globals.cpp
#include "globals.h"
HANDLE hIOMutex = INVALID_HANDLE_VALUE;

// main.cpp
#include "globals.h"
int main ()
{
    // initialize global variables.
    hIOMutex = CreateMutex (NULL,FALSE,NULL);
    // main code.
    while (1) {
        Sleep(STEP);
        WaitForSingleObject(hIOMutex,INFINITE);
        //DO STUFF
        ReleaseMutex(hIOMutex);
    }
}

// case.cpp
#include "case.h"
#include "globals.h"
void Case::account_login(Connection* Con)
{
    WaitForSingleObject(hIOMutex,INFINITE);
    //DO STUFF
    ReleaseMutex(hIOMutex);
}


I would use a named mutex especially if the two places where the mutex is used are in different places. However, ensure you create the Mutex_Name in one place to avoid typo mistakes

LPCTSTR lpMutex_Name = <name>;

//mutex handle
HANDLE hIOMutex = CreateMutex (NULL,FALSE,lpMutex_Name);

//main
while (1) {
    Sleep(STEP);

    WaitForSingleObject(hIOMutex,INFINITE);

    //DO STUFF

    ReleaseMutex(hIOMutex);
}
return 0;
And in a function used by the other thread:

void Case::account_login(Connection* Con) {
    //mutex handle
    HANDLE hIOMutex = CreateMutex (NULL,FALSE, lpMutex_Name);

    WaitForSingleObject(hIOMutex,INFINITE);

    //DO STUFF

    ReleaseMutex(hIOMutex);
    return;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜