0
  • 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?

EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59
  • Perhaps you could give a concrete example of what you want to call. For example, it's not safe to call `Free`. If you give a concrete example there's less scope for mis-understanding. Without a concrete example, there's no single answer. Some methods will be fine to call, some will not. It depends. Is that the answer you want? I doubt it very much. – David Heffernan Aug 05 '13 at 13:55

1 Answers1

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I think you misunderstand. TThread inherites from TObject, so It can of course have methods. Thread's itself don't have methods, I should edit the question. – EProgrammerNotFound Aug 05 '13 at 13:52
  • I don't think I misunderstood what you wrote. Perhaps what you wrote was not what you meant. For sure `TThread` has methods. But that's not what you wrote. You wrote "method of the waiting thread". There's a big difference between the class `TThread` and a *thread*. – David Heffernan Aug 05 '13 at 13:54
  • I know that, and you already answered what I wanted to know, you said that is executed in the context of the caller. The TThread is a wrapper of BeginThread (on windows). – EProgrammerNotFound Aug 05 '13 at 13:59
  • OK, so is your question about which thread will run a method of a `TThread` instance? If that's the question then the answer is, as you say above, it runs on whichever thread makes the call. – David Heffernan Aug 05 '13 at 14:00
  • Yes, that's exactly what I wanted to know. Now, I realise that I have the incredible ability to make question very much confused to tiny problems – EProgrammerNotFound Aug 05 '13 at 14:03
  • Thanks @David, I'm was messy with question, actually I believe is because of lack of fluency in English language, that sometimes make I choose words that has different meaning for fluent people – EProgrammerNotFound Aug 05 '13 at 14:09
  • No probs. I'm glad I was able to help even if only a little. – David Heffernan Aug 05 '13 at 14:10