如何在squeak smalltalk中使用多线程?
我想知道如何在squeak smalltlak中使用线程如何在squeak smalltalk中使用多线程?,smalltalk,squeak,Smalltalk,Squeak,我想知道如何在squeak smalltlak中使用线程 b1 := Ball new. b2 := Ball new. 接下来的两个对象应该在不同的线程中一起运行(多线程)。 我怎么做 "Thread 1" b1 start:210 at:210. "start is the name of the method" "Thread 2" b2 start:310 at:210. 首先,Squeak虚拟机只提供绿色线程,即虚拟机在单个进程中运行,线
b1 := Ball new.
b2 := Ball new.
接下来的两个对象应该在不同的线程中一起运行(多线程)。
我怎么做
"Thread 1"
b1 start:210 at:210. "start is the name of the method"
"Thread 2"
b2 start:310 at:210.
首先,Squeak虚拟机只提供绿色线程,即虚拟机在单个进程中运行,线程在单个进程中模拟 要使用线程(在Squeak中简称为进程),通常将消息
#fork
或#forkAt:
发送到块:
[ b1 start: 210 at: 210 ] fork.
[ b1 start: 210 at: 210 ] forkAt: Processor userBackgroundPriority.
这就是它的全部,除非您需要用于进程间通信的设施。然后,您可以对关键部分使用互斥
(一次只能在该部分中使用一个进程),或使用信号灯
控制对共享资源的访问:
"before critical section"
self mutex critical: [ "critical section" ].
"after critical section"
"access shared resource"
self semaphore wait.
"do stuff..."
"release shared resource"
self semaphore signal.
方法#semaphore
和#mutex
只是变量的访问器。这些变量不应延迟初始化,而应在多个过程调用方法之前初始化。这通常意味着您将使用#initialize
方法初始化它们:
initialize
semaphore := Semaphore new.
mutex := Mutex new.
原因是您不能保证进程不会挂起在\ifNil:
块中。这可能导致两个进程使用两个不同的互斥体/信号量
如果你需要更多的信息,你应该看看这本书,也许可以读一下阿黛尔·戈德堡(Adele Goldberg)的Smalltalk原著(在你最喜欢的网上书店可以买到)。当然,你应该注意你的朋友之间的互动
您可能不需要线程,也可以使用单步执行。backgroundPriority出现错误,原因是什么?他提供:BackgroundProcess或UserBackgroundPriority是的,对不起。这应该是
#userBackgroundPriority
。答案是固定的。