Difference between revisions of "Multi-threading: A brief introduction(多线程简介)"

From wiki.visual-prolog.com

(Created page with "Multi-threading is only possible in the '''Commercial Edition'''. Threads are represented by the <vp>thread</vp> class/interface. And it is very simple to create a new thread: ...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Multi-threading is only possible in the '''Commercial Edition'''.
+
(以下内容译自[[:Category:Tutorials]]中的Multi-threading: A brief introduction。)
  
Threads are represented by the <vp>thread</vp> class/interface.  And it is very simple to create a new thread:
+
只有 ''商业版'' 才支持多线程应用。
 +
 
 +
线程是由 <vp>thread</vp> /接口表示的。创建一个新线程很简单:
  
 
<vip>clauses
 
<vip>clauses
 
         ...
 
         ...
         T = thread::start(myThread), % start a new thread which executes the predicate myThread
+
         T = thread::start(myThread), % 启动一个新线程,这个线程执行谓词 myThread
 
         ...
 
         ...
  
Line 14: Line 16:
 
         ...</vip>
 
         ...</vip>
  
Using anonymous predicates it is also very simple to transfer data to the new thread:
+
使用匿名谓词向新线程传递数据也很简单:
  
 
<vip>clauses
 
<vip>clauses
Line 28: Line 30:
 
         ...</vip>
 
         ...</vip>
  
You can wait for thread termination, because a thread object is a synchronization object:
+
可以等待线程结束,因为线程对象是同步对象:
  
 
<vip>clauses
 
<vip>clauses
Line 34: Line 36:
 
         SomeData = ...,
 
         SomeData = ...,
 
         T = thread::start( { :- myThreadWithData(SomeData) } ),
 
         T = thread::start( { :- myThreadWithData(SomeData) } ),
         ... % stuff done in parallel with T
+
         ... % 与T并行着做某些工作
         T:wait(),  % Wait for T to terminate
+
         T:wait(),  % 等待T结束
 
         ...</vip>
 
         ...</vip>
  
Thread safe acces to shared data can be done using [[Language Reference/Monitors|monitors]] and/or synchronization objects like <vp>criticalSection</vp>, <vp>event</vp>, <vp>mutex</vp> and <vp>semaphore</vp>.
+
要使线程安全访问共享的数据,可以使用 [[Language Reference/Monitors|monitors]] /或同步对象如: <vp>criticalSection</vp><vp>event</vp><vp>mutex</vp> <vp>semaphore</vp>
  
 
=== 参考 ===
 
=== 参考 ===

Latest revision as of 03:29, 22 June 2015

(以下内容译自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结束
        ...

要使线程安全访问共享的数据,可以使用 monitors 和/或同步对象如: criticalSectioneventmutexsemaphore

参考

Thread safety in Visual Prolog