Domanda

  • When a TThread enters in Synchronized() method, it waits until EnterCriticalSection(ThreadLock) returns.

Now, which one will run the method if in the meantime, another Tthread, or even the main thread call some method of the waiting Tthread?

È stato utile?

Soluzione

What could happen if in the meantime, another thread, or even the main thread call some method of the waiting thread?

Threads do not have methods, so this question is a non-sequitur.

It is not meaningful to ask what happens when you call a method of another thread. Because it is not possible to do so. When you call a method, that method executes on thread which called it.

A method like TThread.Synchronize schedules the execution of code onto a different thread. But, the body of TThread.Synchronize is executed by the thread of the caller.

A call to EnterCriticalSection cannot be interrupted by user mode code. So, suppose that thread A calls EnterCriticalSection at a point where thread B holds the lock. The call to EnterCriticalSection made on thread A will not return until thread B has released the lock. While thread A is blocked waiting to acquire the lock, no code will execute on thread A.


It seems, from clarifications in the comments, that your question is in fact:

When a method of TThread is called, on which thread does that method execute?

The answer is that the method is executed on the calling thread. There's nothing special about the TThread class and so the normal rules apply.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top