Multithreading 使线程在其他对象中等待条件
如果我想限制可以同时在一个词典中进行的线程查找的数量,如何做到这一点?让线程休眠,直到它们能够访问词典。即,如果两个查找已并行进行,则其他查找必须等待,直到查找计数小于2时才能执行Multithreading 使线程在其他对象中等待条件,multithreading,delphi,freepascal,lazarus,Multithreading,Delphi,Freepascal,Lazarus,如果我想限制可以同时在一个词典中进行的线程查找的数量,如何做到这一点?让线程休眠,直到它们能够访问词典。即,如果两个查找已并行进行,则其他查找必须等待,直到查找计数小于2时才能执行 TLexicon MaxLookups: Integer; LookupCount: Integer; 除了TLexicon之外,我还创建了TLookupJob,它可以在TLookupThread中执行。Execute过程调用词典上的lookup并等待响应 因此,LookupThread必须休眠,直到Lex
TLexicon
MaxLookups: Integer;
LookupCount: Integer;
除了TLexicon之外,我还创建了TLookupJob,它可以在TLookupThread中执行。Execute过程调用词典上的lookup并等待响应
因此,LookupThread必须休眠,直到Lexicon.LookupCount小于Lexicon.MaxLookups。为此,我在LookupJob.Execute中调用GoSleep(事件),LookupThread的执行将停止。但如何以及何时发出信号。我不清楚设计的其余部分
这里可以使用什么设计
到目前为止,我已经创建了许多类:
TLexicon-具有MaxLookups和LookupCount,以及一个查找函数
LookupItem-包含查找词和响应。传递到Lexicon.Lookup
TLookupJob-有一个执行过程,加上GoSleep和一个使用事件进行睡眠的唤醒过程
LookupThread-执行LookupJob
请随意更改设计。如果您针对的是Windows平台,WinAPI提供了合适的同步功能。看见(当然,独立于平台的选项可能是首选,但这在某些环境中可能仍然很有用。) 创建/初始化词典时,请按如下方式创建一个词库:
FSemaphoreHandle := CreateSemaphore(nil, MaxLookups, MaxLookups, nil);
然后在执行查找时,使用信号量限制同时进入查找例程主体的线程数
function TLexicon.Lookup(...): ...;
begin
//The wait function will block if the current counter = 0 and wait
//until another thread releases a counter.
WaitForSingleObject(FSemaphoreHandle, <timeout>);
//When the wait function returns (assuming due to semaphore count
//available and not timeout), the current counter of the semaphore
//is decreased. This mechanism limits the number of threads that
//can get past the wait funtion.
try
//Do lookup
finally
//By releasing the semaphore, the current counter is increased again,
//allowing a blocked thread to get past the wait function.
ReleaseSemaphore(FSemaphoreHandle, 1, nil);
end;
end;
function.Lookup(…):。。。;
开始
//如果当前计数器=0并等待,等待功能将阻塞
//直到另一个线程释放计数器。
WaitForSingleObject(FSemaphoreHandle,);
//等待函数返回时(假设由于信号量计数
//可用且不超时),信号量的当前计数器
//减少了。此机制限制了所需的线程数
//可以通过等待功能。
尝试
//查找
最后
//通过释放信号量,当前计数器再次增加,
//允许阻塞线程通过等待函数。
释放信号量(FSemaphoreHandle,1,nil);
结束;
结束;
最后,当你读完词典的时候,别忘了
注意:这是一个简单的例子。不要忘记添加适当的错误检查。例如,WaitForSingleObject表示是否可以获取信号量。如果您针对的是Windows平台,WinAPI提供了合适的同步功能。看见(当然,独立于平台的选项可能是首选,但这在某些环境中可能仍然很有用。) 创建/初始化词典时,请按如下方式创建一个词库:
FSemaphoreHandle := CreateSemaphore(nil, MaxLookups, MaxLookups, nil);
然后在执行查找时,使用信号量限制同时进入查找例程主体的线程数
function TLexicon.Lookup(...): ...;
begin
//The wait function will block if the current counter = 0 and wait
//until another thread releases a counter.
WaitForSingleObject(FSemaphoreHandle, <timeout>);
//When the wait function returns (assuming due to semaphore count
//available and not timeout), the current counter of the semaphore
//is decreased. This mechanism limits the number of threads that
//can get past the wait funtion.
try
//Do lookup
finally
//By releasing the semaphore, the current counter is increased again,
//allowing a blocked thread to get past the wait function.
ReleaseSemaphore(FSemaphoreHandle, 1, nil);
end;
end;
function.Lookup(…):。。。;
开始
//如果当前计数器=0并等待,等待功能将阻塞
//直到另一个线程释放计数器。
WaitForSingleObject(FSemaphoreHandle,);
//等待函数返回时(假设由于信号量计数
//可用且不超时),信号量的当前计数器
//减少了。此机制限制了所需的线程数
//可以通过等待功能。
尝试
//查找
最后
//通过释放信号量,当前计数器再次增加,
//允许阻塞线程通过等待函数。
释放信号量(FSemaphoreHandle,1,nil);
结束;
结束;
最后,当你读完词典的时候,别忘了
注意:这是一个简单的例子。不要忘记添加适当的错误检查。例如,WaitForSingleObject表示是否可以获取信号量。我认为这就是我们的目的。FreePascal有一个可以帮助您处理信号量的工具。是的,这正是我所需要的。(请注意,syncobjs是在FPC中的threadmanager之上实现的。使用这些工具,它还可以与(稍后?)Delphis兼容)我想这就是它的用途。FreePascal有一个可以帮助您处理信号量的工具。是的,这正是我所需要的。(请注意,syncobjs是在FPC中的threadmanager之上实现的。使用这些工具,它还可以兼容(稍后?)Delphis提供的封装Windows信号量API的Delphi产品。@TOndrej很高兴知道。它是什么时候推出的?它在D2007中不可用。@Craigyong我想是XE2,但我不确定它可能更早。XE8中的
SyncObjs
单元显然甚至是跨平台的,包括TSemaphore
和其他同步对象。Delphi提供了封装Windows信号API的功能。@TOndrej很高兴知道。它是什么时候推出的?它在D2007中不可用。@CraigYoung我认为XE2可能更早,但我不确定。XE8中的SyncObjs
单元显然甚至是跨平台的,包括TSemaphore
和其他同步对象。