我希望有两个程序可以相互调用,或者从运行的任何线程调用,但一次只运行一个.我怎样才能做到这一点?这会正常吗? var cs: TCriticalSection;procedure a;begin cs.Acquire; try // Execute single thread
var cs: TCriticalSection; procedure a; begin cs.Acquire; try // Execute single threaded here. finally cs.Release; end; end; procedure b; begin cs.Acquire; try // Execute single threaded here. Maybe with calls to procedure a. finally cs.Release; end; end;是的,那会有效.过程A可以在同一个线程中调用B,反之亦然,当线程A使用过程A或B时,线程B必须等待它想要使用这些过程.
请参阅有关关键部分的MSDN文档:http://msdn.microsoft.com/en-us/library/ms682530%28VS.85%29.aspx
关键部分可以嵌套,但是每次调用Acquire都必须调用Release.因为您在try .. finally子句中进行了Release调用,所以确保发生这种情况,因此您的代码很好.