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

  • Home
  • SEARCH
  • 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 7445815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:07:50+00:00 2026-05-29T12:07:50+00:00

I am really at a loss here. i read the MSDN articles thoroughly but

  • 0

I am really at a loss here.
i read the MSDN articles thoroughly but still cant figure out what is going on.
my problem is this: when running a few processes with the following code all is working:

HANDLE m = CreateMutex(NULL, FALSE, L"MyMutex");
char c[20] = "2print";
for(int iter = 0; iter<100; ++iter) {
    WaitForSingleObject(m,INFINITE);
    for(int i=0;i<(int)strlen(c);++i) {
           for(int j=0;j<10000;++j)
                 printf("%c",c[i]);
    }
    BOOL ok = ReleaseMutex(m);
}
CloseHandle(m);

that is, the different processes each printing at its own turn and releasing the mutex until all printing is done.

BUT changing the CreateMutex to: (bInitialOwner from FALSE to TRUE)

HANDLE m = CreateMutex(NULL, TRUE, L"MyMutex");

the first creator will not release the mutex! other processes are just sitting there.
what amazed me was, that by adding an additional releaseMutex, that is changing:

BOOL ok = ReleaseMutex(m);

into:

BOOL ok = ReleaseMutex(m);
ok = ReleaseMutex(m);

make it work!

i am really confused, why wouldnt the first creator release the mutex correctly?
i tried printing all the errors using GetLastError, and what i get seem reasonable, like ERROR_ALREADY_EXISTS for the creators following the first one, which is just what i expect (MSDN says that in this situation the bInitialOwner is simply ignored).

  • 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-29T12:07:50+00:00Added an answer on May 29, 2026 at 12:07 pm

    When you use bInitialOwner=TRUE, the mutex creator automatically acquires the mutex. Then when you call WaitForSingleObject, it acquired the mutex again. Since win32 mutexes are recursive mutexes, you must release once for each time you acquired the mutex – so two ReleaseMutex calls are needed for the initial creator (however every other thread must only release once!)

    You can avoid this by either not using bInitialOwner, or by skipping the WaitForSingleObject on the first loop only if any only if GetLastError() != ERROR_ALREADY_EXISTS. You will need to call SetLastError(0) prior to CreateMutex to clear the error code if you choose the latter.

    If you only need bInitialOwner for some kind of initial setup, it will simplify your code if you drop the mutex prior to entering the common loop. Otherwise, I would strongly recommend simply not using bInitialOwner, unless you have a compelling reason to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really ripping my hair out on this one. I have a JAAS Authentication Provider
I am really at a loss here, and have been trying to find a
I read all over the Amazon documentations, but I'm still confused or should I
I've already got this to work but it's a really bad approach and i
I have a really weird issue that I can't figure out with comparing objects
Really stupid question, sorry, but I can't find it on google (I'm sure it's
Really simple problem: I want to split a connection string into its keyword /
Really basic question I'm sure for some of you Java heads out there. I
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
Really wish i was God, then I would understand what this means: The element

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.