Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6655691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:33:04+00:00 2026-05-26T01:33:04+00:00

I’m trying to implement mutual exchange on a multi-threaded program. I’m not sure if

  • 0

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.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:33:05+00:00Added an answer on May 26, 2026 at 1:33 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.