アットウィキロゴ

Example17.8

「Example17.8」の編集履歴(バックアップ)一覧に戻る

Example17.8 - (2011/02/24 (木) 09:15:05) のソース

#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
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。