请指点一下EnterCriticalSection(20分)

  • 主题发起人 主题发起人 yhl1118
  • 开始时间 开始时间
Y

yhl1118

Unregistered / Unconfirmed
GUEST, unregistred user!
我定义了两个TRTLCriticalSection,在线程中使用<br>var<br>&nbsp; &nbsp; g_RTLCriticalSection_1, g_RTLCriticalSection_2 : TRTLCriticalSection;<br>&nbsp; &nbsp; g_count_1, g_count_2 : integer;<br>//1.由于程序总出现问题,所以想确定一下是不是这种写?<br>procedure TMyThread.Execute;<br>begin<br>&nbsp; &nbsp; EnterCriticalSection(g_RTLCriticalSection_1);<br>&nbsp; &nbsp; inc(g_count_1);<br>&nbsp; &nbsp; LeaveCriticalSection(g_RTLCriticalSection_1);<br>end;<br>//2.能否在一个TRTLCriticalSection中使用另一个TRTLCriticalSection<br>EnterCriticalSection(g_RTLCriticalSection_1);<br>inc(g_count_1);<br>EnterCriticalSection(g_RTLCriticalSection_2);<br>inc(g_count_2);<br>LeaveCriticalSection(g_RTLCriticalSection_2);<br>LeaveCriticalSection(g_RTLCriticalSection_1);<br><br>请指点一下,谢谢!
 
直接使用CriticalSection吧。Delphi帮助中有个例子。
 
1. OK, but in try..finally..end block is better.<br>procedure TMyThread.Execute;<br>begin<br>&nbsp; &nbsp;EnterCriticalSection(g_RTLCriticalSection_1);<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp;inc(g_count_1);<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp;LeaveCriticalSection(g_RTLCriticalSection_1);<br>&nbsp; &nbsp;end;<br>end;<br>2. OK, but first you should ensure order of using g_RTLCriticalSection_1 and <br>g_RTLCriticalSection_2 in other codes. if other codes like follows, it may<br>enter into dead-lock.<br>EnterCriticalSection(g_RTLCriticalSection_2);<br>try<br>&nbsp; inc(g_count_1);<br>&nbsp; EnterCriticalSection(g_RTLCriticalSection_1);<br>&nbsp; try<br>&nbsp; &nbsp; inc(g_count_2);<br>&nbsp; finally<br>&nbsp; &nbsp; LeaveCriticalSection(g_RTLCriticalSection_1);<br>&nbsp; end;<br>finally<br>&nbsp; LeaveCriticalSection(g_RTLCriticalSection_2);<br>end;<br>
 
你的写法没有问题,另外不要加TRY FINALLY,如果中间代码只是给一个变量加1的话,那样会增加处理负担的,且没有任何含义
 
感谢大家,我看了Delphi中的例子,<br>我现在有点疑问,可能是我没有理解对, 我一直是将 TRTLCriticalSection 变量定义成全局的,可是delphi中的例子总是定义在private中,如下的FLock, 这让我没有明白,我原来认为只能将TCriticalSection定义成全局的,才能对所有的线程起作用.<br><br>TComponentImageFileList = class(TObject)<br>&nbsp; private<br>&nbsp; &nbsp; FFileLists: TObjectList;<br>&nbsp; &nbsp; FComponentNames: TStrings;<br>&nbsp; &nbsp; FFileID: Integer;<br>&nbsp; &nbsp; FLock: TCriticalSection;<br>&nbsp; <br>是我理解错的吗??? 如果定义在private中,当两个线程同时运行到FLock.enter的地方时,会进行locking吗?<br>另外:如果在两处代码(做同样的操作,如改变一个全局变量的值)要进行locking,是不是可以用同一个FLock, 会不会造成死锁, 这里的问题困惑了我很久,请大家帮帮忙,谢谢!
 
请指点一下,着急!!!
 
来个人,指点指点吧!!!
 
procedure TMultiTrdCDSGet.DoAddSub;<br>var<br>&nbsp; ThreadL:TCriticalSection;<br>begin<br>&nbsp; if FPrepareThread&lt; FThreadCount then FPrepareThread := FThreadCount ;<br>&nbsp; if FPrepareThread &gt; FTotalThread then<br>&nbsp; begin<br>&nbsp; &nbsp; Terminate;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; ThreadL := TCriticalSection.Create;<br>&nbsp; &nbsp; ThreadL.Acquire;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; FPrepareThread:=FPrepareThread+1;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; ThreadL.Release;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CreateNewGet((FPrepareThread-1)*FPacketRec+1,FPacketRec);;<br>&nbsp; end;<br>end;<br><br>这样行不行?
 
//1.由于程序总出现问题,所以想确定一下是不是这种写?<br>没错!<br>//2.能否在一个TRTLCriticalSection中使用另一个TRTLCriticalSection<br>没错!
 
请你把程序的问题说得详细一点。
 
后退
顶部