DMD 1.022 and 2.005 releases - what's wrong with threading?

downs default_357-line at yahoo.de
Sat Oct 6 23:11:30 PDT 2007


Sure, on it.
This should work .. at a glance, I wasn't able to find any cases where
it deadlocks.
I made an unbounded buffer, because it seemed easier ^_____^

module test4;

import std.thread;
class UnBoundedBuffer(T) {
  private {
    T[] buffer;
    bool[Thread] waiting;
    Object waiting_sync;
  }
  this() { waiting_sync=new Object; }
  void put(T t)
  {
    synchronized(this) buffer~=t;
    while (waiting.length && buffer.length)
      synchronized(waiting_sync)
        foreach (thr, bogus; waiting)
          thr.resume;
  }
  T get()
  {
    synchronized(waiting_sync) waiting[Thread.getThis]=true;
    scope(exit) synchronized(waiting_sync) waiting.remove(Thread.getThis);
    while (true)
    {
      synchronized(this)
        if (buffer.length)
        {
          auto res=buffer[0];
          buffer=buffer[1..$];
          return res;
        }
      Thread.getThis.pause;
    }
  }
}

void main() {
  auto bb=new UnBoundedBuffer!(int);
}



More information about the Digitalmars-d mailing list