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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:25+00:00 2026-05-26T05:54:25+00:00

So far I have this code: ****SimpleForm.h**** class TForm1 : public TForm { __published:

  • 0

So far I have this code:

****SimpleForm.h****
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TMemo *Memo1;
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
private:    // User declarations
    TMutex *mtx;
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
};

****SimpleForm.cpp****
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    mtx = new TMutex(true);
    WorkerThread *wt = new WorkerThread(false, mtx);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    mtx->Acquire();
        Memo1->Lines->Add("Locked...");
    mtx->Release();
}

****WorkerThread.h****
class WorkerThread : public TThread
{
private:
    TMutex *mtx;
protected:
    void __fastcall Execute();
public:
    __fastcall WorkerThread(bool CreateSuspended, TMutex *mtx);
    void __fastcall CheckLock();
};
****WorkerThread.cpp****
__fastcall WorkerThread::WorkerThread(bool CreateSuspended, TMutex *mtx)
    : TThread(CreateSuspended)
{
    this->mtx = mtx;
}

void __fastcall WorkerThread::Execute()
{
    while(true){
        Sleep(1000);
        Synchronize(CheckLock);
    }

}

void __fastcall WorkerThread::CheckLock(){
    this->mtx->Acquire();
    Form1->Memo1->Lines->Add("Locked from thread");
    //this->mtx->Release();
}

The problem is, mtx->Acquire() does not lock a mutex, when I comment the mtx->Release(), nothing changes during runtime, both threads can access the same shared resource at the same time, witch is not what I want. I was using p_threads in Linux environment, and when the mutex is locked, other threads waits for it to become available. How can I achieve the same result using C++ CodeGear 2009 ?

  • 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-26T05:54:26+00:00Added an answer on May 26, 2026 at 5:54 am

    Something else is the explanation to your problem because TMutex::Acquire does indeed acquire a lock on the mutex object. The implementation of TMutex looks like this:

    procedure TMutex.Acquire;
    begin
      if WaitFor(INFINITE) = wrError then
        RaiseLastOSError;
    end;
    
    procedure TMutex.Release;
    begin
      if not ReleaseMutex(FHandle) then
        RaiseLastOSError;
    end;
    

    And WaitFor calls WaitForMultipleObjectsEx passing the mutex handle.

    Most likely is that you actually have more than one mutex somehow but I can’t tell for sure since I can’t see all of your code.

    Finally, for within process synchronisation you should prefer the Windows critical section which performs better than the Windows mutex object. That is TCriticalSection in the RTL.


    Following your update it is easy to see what is happening. All use of the lock happens in the main thread. You call Synchronize which results in the method executing on the main thread. If you call CheckLock directly from your Execute method then you will deadlock as intended.

    You need to use Synchronize for all GUI calls. It works, broadly, by signaling the main thread that there is something in the synchronize queue and then waiting until the main thread has completed the work. It is an asynchronous method.

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

Sidebar

Related Questions

I have this code so far which perfectly but relies on there being a
This is what I have as far as code, it is in the first
This is what I have so far - my dropbox public URL creation script
So far I have this code run when the X button is clicked, but
So far I have this code: NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in
I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>
ok so i have this code so far: for (int i = 0; i
So far, I have this code sample: ... int nbytes =0; vector<unsigned char> buffer;
So far i have this code: function changeGeoLoc($a,$b,$c){ echo <ul style='list-style-type: none; display:inline;'>; while(list(,$geoloc)
so far i have used this code.It is working when i am sending the

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.