Creating a future/promise object

thedeemon via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 6 22:56:22 PDT 2015


On Monday, 6 July 2015 at 20:56:04 UTC, Frank Pagliughi wrote:
>     void set_result(int retCode) {
>       synchronized (mut) {
>         this.retCode = retCode;
>         completed = true;
>         cond.notify();
>       }
>     }
>
>     int get_result() {
>       synchronized (mut) {
>         while (!completed)
>           cond.wait();
>         return retCode;
>     }
>   }
> Any help would be appreciated.

A bit of offtopic:
1) you don't need a mutex here, you can just use the token object 
itself in "synchronized".
2) I see a deadlock here: you're waiting for the job to be 
completed but it cannot notify() completion because it cannot 
enter set_result() because you took the mutex in the waiting 
thread.


More information about the Digitalmars-d mailing list