Multi-threading: A brief introduction(多线程简介)
From wiki.visual-prolog.com
(以下内容译自Category:Tutorials中的Multi-threading: A brief introduction。)
只有 商业版 才支持多线程应用。
线程是由 thread 类/接口表示的。创建一个新线程很简单:
clauses ... T = thread::start(myThread), % 启动一个新线程,这个线程执行谓词 myThread ... predicates myThread : (). clauses myThread() :- ...
使用匿名谓词向新线程传递数据也很简单:
clauses ... SomeData = ..., T = thread::start( { :- myThreadWithData(SomeData) }), ... predicates myThreadWithData : (complexData ThreadInputData). clauses myThreadWithData(ThreadInputData) :- ...
可以等待线程结束,因为线程对象是同步对象:
clauses ... SomeData = ..., T = thread::start( { :- myThreadWithData(SomeData) } ), ... % 与T并行着做某些工作 T:wait(), % 等待T结束 ...
Thread safe acces to shared data can be done using monitors and/or synchronization objects like criticalSection, event, mutex and semaphore.