A ahjie Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-23 #1 如果没有,该怎么实现呢? 我想写一个方法,每次只能有一个线程能够访问这个方法。 兄弟们,这个怎么解决啊? 钱不多,大家帮帮忙~~
D dedema Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-23 #2 容易啊! uses SyncObjs; var Lock:TCriticalSection Lock:=TCriticalSection.Create; Lock.Enter; //只想让一个线程访问的代码。 Lock.Leave;
容易啊! uses SyncObjs; var Lock:TCriticalSection Lock:=TCriticalSection.Create; Lock.Enter; //只想让一个线程访问的代码。 Lock.Leave;
A ahjie Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-23 #3 fuction myfuction(n:integer):integer var Lock:TCriticalSection; begin Lock:=TCriticalSection.Create; Lock.enter; ... //方法代码; Lock.leave; end; 是这样么????
fuction myfuction(n:integer):integer var Lock:TCriticalSection; begin Lock:=TCriticalSection.Create; Lock.enter; ... //方法代码; Lock.leave; end; 是这样么????
Z zzsczz Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-23 #4 to ahjie Lock.leave; 后面要加 lock.free 最保险的是调用时保护 ; Lock.enter; myfuction(xxx) //保护调用 Lock.leave; 因为 ; fuction myfuction(n:integer):integer var Lock:TCriticalSection; begin Lock:=TCriticalSection.Create; Lock.enter; ... //方法代码; Lock.leave; //此处可能会发生进程切换。。。 end;
to ahjie Lock.leave; 后面要加 lock.free 最保险的是调用时保护 ; Lock.enter; myfuction(xxx) //保护调用 Lock.leave; 因为 ; fuction myfuction(n:integer):integer var Lock:TCriticalSection; begin Lock:=TCriticalSection.Create; Lock.enter; ... //方法代码; Lock.leave; //此处可能会发生进程切换。。。 end;
迷 迷糊 Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-23 #6 临界区的声明和初始化是不是应该放在方法外边啊? var Lock:TCriticalSection; Lock:=TCriticalSection.Create;