Overview of TCriticalSection and Synchronize

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

As far as I know, Synchronize now uses SendMessage (updates: or at least uses it in older versions of VCL, as mentioned in a few comments below), which pauses the execution of the current thread (like any other thread) unlike PostMessage , which does not and does not perform the required function (from the main thread). In the SendMessage method, multithreading is stopped during execution.

But I'm not sure about TCriticalSection . Say, for example, I am creating 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 does the critical section โ€œknowโ€ that I protect MyVariable1 in this case, and also regardless of what the called function can change?

If I understood it correctly - this is not so - and I must correctly call Acquire () on any thread that wants to change MyVariable1 or call this function (or make either of the two). In other words, I think of TCriticalSection as a user block that defines what I logically assigned to it. It can be a set of variables or any specific function if I call Acquire () on all threads that can write to this block or use this function. For example, โ€œDiskOpโ€ might be my name TCriticalSection , which writes to disk, โ€œInternetโ€ might be the name TCriticalSection , which calls functions that extract some data from the Internet. Did I understand correctly?

Also, in this context, should TCriticalSection always be a global kind of variable?

+7
source share
1 answer

SendMessage pauses the execution of the current thread (like any other thread).

No, this is not true. SendMessage does not pause anything. SendMessage simply sends the message synchronously. The function is not returned until the message is delivered. That is, the windowing process of the target window was completed. And since the proc window is always called in the thread that owns the window, this means that the calling thread may need to be blocked in order to wait until the thread that owns the window is ready to execute the proc window. This definitely does not stop all threads in the process.

How does a critical section know that I am MyVariable1 ?

This is not true. It is entirely up to you to protect all uses of MyVariable1 that need protection. The critical section is a form of a mutex. A mutex ensures that only one thread of execution can hold the mutex at any given time.

As I call Acquire() in all threads that can write to this block or use this function.

This is also not so. "Inside all threads " is incorrect. You should be thinking about "in all sections of the code that use the variable."

Therefore, should a critical section always be a global kind of variable?

No, a critical section may be a global variable. But this is optional.

+14
source

All Articles