「Example17.8」の編集履歴(バックアップ)一覧はこちら
Example17.8 - (2011/02/24 (木) 09:15:05) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
#co(){
17.8 Synchronous Channels
Here's an implementation of synchronous channels, where the sender of a message blocks until that message has been received. Synchronous channels only need a single variable to store messages in transit, but three signals are used to coordinate reader and writer processes.
}
** 17.8 同期チャネル
同期チャネルの実装例を見てみましょう。ここではメッセージの送信者はメッセージが受け取られるまでブロックします。同期チャネルは、転送するメッセージを保持するのにただ1つの変数を必要としますが、リーダーとライタープロセスをうまく連動させるために3つの信号を使います。
package scala.concurrent
class SyncChannel[A] {
private var data: A = _
private var reading = false
private var writing = false
def write(x: A) = synchronized {
while (writing) wait()
data = x
writing = true
if (reading) notifyAll()
else while (!reading) wait()
}
def read: A = synchronized {
while (reading) wait()
reading = true
while (!writing) wait()
val x = data
writing = false
reading = false
notifyAll()
x
}
}
#center(){[[前ページ>Example17.7]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.9]]}
----
#comment
#co(){
17.8 Synchronous Channels
Here's an implementation of synchronous channels, where the sender of a message blocks until that message has been received. Synchronous channels only need a single variable to store messages in transit, but three signals are used to coordinate reader and writer processes.
}
#setmenu2(ex-r-menu)
** 17.8 同期チャネル
同期チャネルの実装例を見てみましょう。ここではメッセージの送信者はメッセージが受け取られるまでブロックします。同期チャネルは、転送するメッセージを保持するのにただ1つの変数を必要としますが、リーダーとライタープロセスをうまく連動させるために3つの信号を使います。
package scala.concurrent
class SyncChannel[A] {
private var data: A = _
private var reading = false
private var writing = false
def write(x: A) = synchronized {
while (writing) wait()
data = x
writing = true
if (reading) notifyAll()
else while (!reading) wait()
}
def read: A = synchronized {
while (reading) wait()
reading = true
while (!writing) wait()
val x = data
writing = false
reading = false
notifyAll()
x
}
}
#center(){[[前ページ>Example17.7]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.9]]}
----
#comment