diff options
author | Hubert Plociniczak <hubert.plociniczak@epfl.ch> | 2011-11-02 14:34:35 +0000 |
---|---|---|
committer | Hubert Plociniczak <hubert.plociniczak@epfl.ch> | 2011-11-02 14:34:35 +0000 |
commit | b6778be91900b8161e705dc2598ef7af86842b0b (patch) | |
tree | d15e8ec18a37eec212f50f1ace27714d7e7d4d34 /test/pending/shootout/prodcons.scala | |
parent | ac6c76f26d884a94d0c9ff54f055d3f9ab750bac (diff) | |
download | scala-b6778be91900b8161e705dc2598ef7af86842b0b.tar.gz scala-b6778be91900b8161e705dc2598ef7af86842b0b.tar.bz2 scala-b6778be91900b8161e705dc2598ef7af86842b0b.zip |
Begone t1737...
Diffstat (limited to 'test/pending/shootout/prodcons.scala')
-rw-r--r-- | test/pending/shootout/prodcons.scala | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/test/pending/shootout/prodcons.scala b/test/pending/shootout/prodcons.scala new file mode 100644 index 0000000000..d48d3e94d8 --- /dev/null +++ b/test/pending/shootout/prodcons.scala @@ -0,0 +1,64 @@ +/* The Computer Language Shootout + http://shootout.alioth.debian.org/ + contributed by Isaac Gouy (Scala novice) +*/ + +import concurrent.SyncVar; +import concurrent.ops._; + +object prodcons { + def main(args: Array[String]) = { + val n = toPositiveInt(args); + val buffer = new SharedBuffer(); + var p = 0; + var c = 0; + val cDone = new SyncVar[Boolean]; + + spawn { + while(p<n) { p=p+1; buffer put(p); } + } + + spawn { + var v: Int = _; + while(c<n) { c=c+1; v = buffer.get; } + cDone set true; + } + + cDone.get; + Console println(p + " " + c); + } + + + private def toPositiveInt(s: Array[String]) = { + val i = + try { Integer.parseInt(s(0)); } + catch { case _ => 1 } + if (i>0) i; else 1; + } +} + + +private class SharedBuffer() { + var contents: Int = _; + var available = false; + + def get = synchronized { + while (available == false) wait(); + available = false; + // Console println("\t" + "get " + contents); + notifyAll(); + contents + } + + def put(value: Int) = synchronized { + while (available == true) wait(); + contents = value; + available = true; + // Console println("put " + value); + notifyAll(); + } +} + + + + |