1 module neton.util.future; 2 import core.sync.condition; 3 import core.sync.mutex; 4 import hunt.logging; 5 6 class Future(Req , Res) 7 { 8 9 Req _in; 10 Res _out; 11 Condition _condition; 12 13 this(Req in_ ) 14 { 15 _in = in_; 16 // trace("**** key : ",cast(string)(_in.key)); 17 _condition = new Condition(new Mutex()); 18 } 19 20 21 Res get() 22 { 23 _condition.mutex().lock(); 24 trace("condition waiting ..."); 25 _condition.wait(); 26 trace("condition waiting done ..."); 27 _condition.mutex().unlock(); 28 return _out; 29 } 30 31 void done(Res out_) 32 { 33 trace("condition notify ..."); 34 _out = out_; 35 _condition.mutex().lock(); 36 _condition.notify(); 37 _condition.mutex().unlock(); 38 } 39 40 Req data() @property 41 { 42 return _in; 43 } 44 45 } 46 47 48 unittest 49 { 50 import core.thread; 51 import std.stdio; 52 auto f = new Future!(string,string)("hello"); 53 new Thread((){ 54 Thread.sleep(dur!"secs"(1)); 55 if(f.data == "hello") 56 f.done("world"); 57 }).start(); 58 writeln(f.get()); 59 }