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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:51:04+00:00 2026-06-15T08:51:04+00:00

I would like to confirm here if I understood correctly how TCriticalSection and Synchronize

  • 0

I would like to confirm here if I understood correctly how TCriticalSection and Synchronize operate.

As far as I know right now Synchronize uses SendMessage (update: or at least used it in older VCL versions as mentioned in couple of comments below) which suspends currently executing thread (as well as any other thread) unlike PostMessage which doesn’t and then executes required function (from main thread). In a way SendMessage “stops” multithreading when executing.

But I am not sure about TCriticalSection. Let’s say for example I create something like this:

// Global variables somewhere in my code any thread can access
boost::scoped_ptr<TCriticalSection> ProtectMyVarAndCallFnction(new TCriticalSection);
int MyVariable1;
void CallMyFunctionThatAlsoModifiesMoreStuff() { /* do even more here */ };


// Thread code within one of the threads
try {
    ProtectMyVarAndCallFnction->Acquire();
    MyVariable1++;
    CallMyFunctionThatAlsoModifiesMoreStuff();
    }
__finally {
    ProtectMyVarAndCallFnction->Release();
    }

Now, my question is – how the critical section “knows” that I am protecting MyVariable1 in this case as well as whatever the called function may modify?

If I understood it correctly – it doesn’t – and it is my responsibility to correctly call Acquire() in any thread wants to change MyVariable1 or call this function (or do any of the two). In other words I think of TCriticalSection as user-defined block which defines whatever logically I assigned to it. It may be a set of variables or any particular function as long as I call Acquire() within all of the threads that might write to this block or use this function. For example “DiskOp” may be my name of TCriticalSection that writes on disk, “Internet” may be the name of TCriticalSection that calls functions that retrieve some data from the Internet. Did I get it correctly?

Also, within this context, does TCriticalSection therefore always needs to be a global kind of variable?

  • 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-06-15T08:51:06+00:00Added an answer on June 15, 2026 at 8:51 am

    SendMessage suspends currently executing thread (as well as any other thread).

    No, that is incorrect. SendMessage does not suspend anything. SendMessage merely delivers a message synchronously. The function does not return until the message has been delivered. That is, the window proc of the target window has been executed. And because the window proc is always called on the thread that owns the window, this means that the calling thread may need to be blocked to wait until the window’s owning thread is ready to execute the window proc. It most definitely doesn’t suspend all threads in the process.

    How does the critical section know that I am protecting MyVariable1?

    It doesn’t. It’s entirely up to you to make sure that all uses of MyVariable1 that need protection, are given protection. A critical section is a form of mutex. A mutex ensures that only one thread of execution can hold the mutex any any instant in time.

    As I call Acquire() within all of the threads that might write to this block or use this function.

    That’s not really it either. The “within all of the threads” is a mis-think. You need to be thinking about “at all sections of code that use the variable”.

    Does a critical section therefore always need to be a global kind of variable?

    No, a critical section can be a global variable. But it need not be.

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

Sidebar

Related Questions

I would like to confirm whether I am on the right track when identifying
I imagine two, but I would like to confirm if I am making sense
I have a confusion regarding DataContext which i would like someone to confirm or
I have a RadioButtonList control and I would like to do a Javascript confirm
and it's linked to a 'confirm comment deleted page. Ideally I would like it
What would a simple unit test look like to confirm that a certain controller
I have been reading about JPA and EJB3 and would like to confirm that
I would like to replace the Javascript confirm() function to allow custom buttons instead
I can't confirm that this syntax is best practice. I would like to reference
I am trying to confirm my understanding of what the code would look 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.