summaryrefslogtreecommitdiff
path: root/sources/examples/buffer1.scala
blob: 40be8431be47b560ca26e4a295ce5089eb0765f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package examples;

import scala.concurrent._;

class OnePlaceBuffer() {
  private val m = new MailBox();             // An internal mailbox
  private case class Empty(), Full(x: Int);  // Types of messages we deal with

  m send Empty();                            // Initialization

  def write(x: Int): Unit =
    m receive { case Empty() => m send Full(x) }

  def read: Int =
    m receive { case Full(x) => m send Empty() ; x }
}