[英]Which thread will run a method of a TThread instance? (In Wait mode)
Synchronized()
method, it waits until EnterCriticalSection(ThreadLock)
returns. Synchronized()
方法时,它会一直等到EnterCriticalSection(ThreadLock)
返回。 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? 现在,如果同时运行该方法,另一个Tthread,甚至主线程调用等待Tthread的某些方法?
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. 像
TThread.Synchronize
这样的方法将代码的执行安排到不同的线程上。 But, the body of TThread.Synchronize
is executed by the thread of the caller. 但是,
TThread.Synchronize
的主体由调用者的线程执行。
A call to EnterCriticalSection
cannot be interrupted by user mode code. 用户模式代码不能中断对
EnterCriticalSection
的调用。 So, suppose that thread A calls EnterCriticalSection
at a point where thread B holds the lock. 因此,假设线程A在线程B持有锁的位置调用
EnterCriticalSection
。 The call to EnterCriticalSection
made on thread A will not return until thread B has released the lock. 在线程B释放锁之前,对线程A的
EnterCriticalSection
调用不会返回。 While thread A is blocked waiting to acquire the lock, no code will execute on thread A. 当线程A被阻塞等待获取锁时,没有代码将在线程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?调用
TThread
的方法时,该方法执行哪个线程?
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. TThread
类没有什么特别之处,因此适用常规规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.