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 6567969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:23:58+00:00 2026-05-25T14:23:58+00:00

I am in a multi-threaded situation and I have a function that I want

  • 0

I am in a multi-threaded situation and I have a function that I want to be run from only one thread at a time. However, rather than serializing the function in the tradition manner, I want any threads that attempt to enter the function whilst the first thread is running it to return immediately. I do not want the second thread to wait for the first thread.

Here is my code:

function InitMutex(const Name:String; var Handle: THandle):Boolean;
begin
 Handle := CreateMutexA(NIL, True, PAnsiChar(Name));
 Result := not (GetLastError = ERROR_ALREADY_EXISTS);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
 mHandle: THandle;
begin
 if not InitMutex(BalloonTipMutex, mHandle) then Exit;


 MessageBox(0, 'Executing Code....', '', 0);


 ReleaseMutex(mHandle);
 CloseHandle(mHandle);
end;

This is just an example with the same problem, cause I couldn’t do a test sample with the threads.

The problem is: I click button1 for the first time, The messagebox appears, while the messagebox is still displayed (suppose the function is still running) I press button1 again, nothing is displayed (which is what’s supposed to happen) but when I close the message box and press the button again, it shows nothing. (the function supposed to run again since its not running :S)

  • 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-25T14:23:59+00:00Added an answer on May 25, 2026 at 2:23 pm

    Try this instead:

    procedure TForm1.Button1Click(Sender: TObject);
    var  mHandle: THandle; 
    begin   
      mHandle := 0;
      if InitMutex(BalloonTipMutex, mHandle) then 
      begin      
        MessageBox(0, 'Executing Code....', '', 0);
        ReleaseMutex(mHandle);  
      end;
      if handle <> 0 then
        CloseHandle(mHandle); 
    end;
    

    your problem is… Even if CreateMutex returns error ERROR_ALREADY_EXISTS, it did “open” the mutex. So when your first function exit, the mutex is not freed since your 2nd call opened it, but never closed it. So when you try to call your function a 3rd time, it fails not because your first call kept the mutex open, but because your 2nd call did.

    Also, I think InitMutex should return Result := (Handle <> 0) and not (GetLastError = ERROR_ALREADY_EXISTS)

    EDIT: On a side note, this isn’t really the way mutex are meant to be used. The “traditional” way to use mutex is to create them, then have your thread try to get ownership of them when you want to execute the code protected by the mutex. I would expect CreateMutex to be quite a bit slower than just taking ownership of a mutex and maybe there are some other pitfalls to that technique.

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

Sidebar

Related Questions

I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
If I have a multi-threaded program that reads a cache-type memory by reference. Can
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
I've run into some problems debugging a multi-threaded process using GDB. I have a
I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I
my perl script is multi threaded and in each thread i have to write
Assume a multi-threaded environment and a (properly synchronized) class that has one particular procedure
I really have a strange situation. I'm making a Linux multi-threaded C application using
Are multi-threaded CLR stored procs possible? I have a data-intensive task with lots of
When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My

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.