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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:49:22+00:00 2026-05-26T20:49:22+00:00

In my project I have a Thread which might be modiefied by the thread

  • 0

In my project I have a Thread which might be modiefied by the thread itself, other thread or VCL (main app).
Thus I’m using TCriticalSection.Acquire / Release for every data access.

Under normal circumstances, the below code works as expected: enters Acquire, Synchronizes with DoCallback, then releases the lock.
However, if any of other contexts Acquires the lock at the time it was already locked, the execution of below code stops at Synchronize – and, this time, it DOES NOT enter the DoCallback method.

Should I skip Synchronize method (even though the Synchronize’d code calls VCL) and rely on the CriticalSection itself? What is the reason of this behavior?

code of the main thread:

  fData:= nil;
  try
    fSingleRequest.Acquire;      
    if fItem <> nil then
      begin
        fData:= fItem.Request;
        SubmitRequest();
        fCallbackData:= fItem.fExtraData;
        fCallback:= fItem.fCallback;
        Synchronize(DoCallback); // <-- this line is called
      end;
  finally
    fSingleRequest.Release;      // <-- this isn't under the specific situation
  end;
  • 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-26T20:49:23+00:00Added an answer on May 26, 2026 at 8:49 pm

    If your so-called “main” thread acquires the critical section, and then the VCL thread (which is what we would usually call the “main” thread) attempts to acquire it, then the VCL thread will block until the critical section is released. Then your “main” thread calls Synchronize, which runs the given function in the context of the VCL thread. Since the VCL thread is blocked waiting on the critical section, it’s not processing messages, so it can’t notice that there’s a synchronized method to call. Thus, deadlock.

    Don’t hold the lock across intra-thread calls. Release the lock in your “main” thread before you call Synchronize, and then re-acquire it afterward, if you still need it. If the data being used in the synchronized method needs continued protection from simultaneous access, then I think you should find a way to copy the data into a separate, non-shared object. Have the synchronized method use that object instead of the shared one, and then destroy it afterward.

    Your comment indicates that you can call the callback function without Synchronize and everything appears to work. In that case, the synchronized method isn’t being called in the same thread context as before. It’s being called directly by your “main” thread instead of by the VCL thread. That obviously relieves the lock contention, but whether it’s really safe depends on what the callback function does.

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

Sidebar

Related Questions

I'm writing a django project, and I need to have a parallel thread which
I have some project where I have a single producer thread which writes events
In my Silverlight project I have a thread that fires every x milliseconds. In
I am playing with Weld-SE. I have a project which has pluggable modules. With
On a small embedded system project we have some code which we would like
I have a silly question which prevents me to proceed with my project. My
I have a netbeans java console project which uses Spring 3.0 ( which is
I have a project using curses and sockets to make a talking program. Basically
I have a thread pool created using java.util.concurrent.ThreadPoolExecutor Is there anyway I can wait
I have a project which I have used Active Record and which I'd like

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.