<div dir="ltr"><div class="gmail_default" style="font-family:'courier new',monospace"><span style="font-family:arial">2014-09-18 17:55 GMT+08:00 Michael Schnell </span><span dir="ltr" style="font-family:arial"><<a href="mailto:mschnell@lumino.de" target="_blank">mschnell@lumino.de</a>></span><span style="font-family:arial">:</span><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><span class="">
<div><br></div></span>
Sven made me know that by "event" you did not mean an object pascal
language "Event" (i.e. a callback), but a "TEvent instance". <br></div></blockquote><div><br></div><div class="gmail_default" style="font-family:'courier new',monospace">Sorry, I mean *neither*. I mean an RTLEvent. See this page:</div><div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style><font face="courier new, monospace"><a href="http://www.freepascal.org/docs-html/rtl/system/rtleventcreate.html">http://www.freepascal.org/docs-html/rtl/system/rtleventcreate.html</a></font></div><div class="gmail_default" style><font face="courier new, monospace"><br></font></div><div class="gmail_default" style><font face="courier new, monospace">What I need help to understand is, repeating my previous email:</font></div><div class="gmail_default" style><font face="courier new, monospace"><br></font></div><div class="gmail_default" style><font face="courier new, monospace">While using ACriticalSection.Acquire, two threads are COMPETING for a "token" to do certain task, hence you do not know WHICH thread eventually get that token, although statistically each thread have the same chance of getting the token in the long run.</font></div><div class="gmail_default" style><font face="courier new, monospace"><br></font></div><div class="gmail_default" style><font face="courier new, monospace">While calling RTLeventWaitFor(),<span style="color:rgb(0,0,0);white-space:nowrap"> the calling thread gives up</span></font></div><div class="gmail_default" style><font face="courier new, monospace"><span style="color:rgb(0,0,0);white-space:nowrap">chance to run, blocked </span></font><span style="color:rgb(0,0,0);white-space:nowrap;font-family:'courier new',monospace">until another thread calls RTLeventSetEvent() to</span></div><div class="gmail_default" style><span style="color:rgb(0,0,0);white-space:nowrap;font-family:'courier new',monospace">notify it. </span></div><div class="gmail_default" style><span style="color:rgb(0,0,0);white-space:nowrap;font-family:'courier new',monospace"><br></span></div><div class="gmail_default" style><span style="color:rgb(0,0,0);white-space:nowrap;font-family:'courier new',monospace">If the above understanding is correct, then I believe only RTLEvent can</span></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">"synchronize" threads, i.e., let them run in a *determined* order. While</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">CriticalSection ONLY provide a way to protect a shared resource is not</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">accessed simultaneously.</span></font></div><div class="gmail_default" style><br></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">Alternatively, I have another "design":</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"><br></span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">procedure TMyThread.Execute;</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">begin</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"> while not Terminated do begin</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"> if FCondition = true then begin</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"> //do the job...</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"> end;</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"> end;</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">end;</span></font></div><div class="gmail_default" style><br></div><div class="gmail_default" style><span style="color:rgb(0,0,0);font-family:'courier new',monospace;white-space:nowrap">In the code above, I do not use event or critical section is it enough to</span><br></div><div class="gmail_default" style><span style="color:rgb(0,0,0);font-family:'courier new',monospace;white-space:nowrap">let this thread wait there, without wasting cpu time, until FCondition is</span></div><div class="gmail_default" style><span style="color:rgb(0,0,0);font-family:'courier new',monospace;white-space:nowrap">set (externally by the main thread)?</span></div><div class="gmail_default" style><span style="color:rgb(0,0,0);font-family:'courier new',monospace;white-space:nowrap"><br></span></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">Thanks!</span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap"><br></span></font></div><div class="gmail_default" style><font color="#000000" face="courier new, monospace"><span style="white-space:nowrap">Xiangrong</span></font></div></div></div></div>