Creating a future/promise object

Frank Pagliughi via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 7 10:46:54 PDT 2015


It seems that just casting the Token (future) as "shared" to get 
it across the thread boundary works. From inside my thread:

   receive(
     (shared(Token) tok) {
       int res = do_something();
       (cast(Token)tok).set_result(res);
       ...

And then in the method which is exposed to the user:

   Token start_something() {
     auto tok = new Token();
     send(tid, cast(shared) tok);
     return tok;
   }

As I showed, the "Token" class is carefully crafted with locks 
and all to have an instance be shared across threads. But will 
this casting back and forth to "shared" do the right thing? Each 
thread now has a non-shared reference to the object.


More information about the Digitalmars-d mailing list