Hello All,
I'm trying to figure out how to create a shared object that can
be used to track asynchronous operations. Something that can be
used like:
Token tok = mything.start_something();
// do something else for a while
int n = tok.get_result(); // Block until result is ready
The 'mything.start_something()' call sends a message to a
spawn'ed thread to begin the operation, which also creates and
sends back a 'Token' which it will signal when the operation is
complete.
I don't see anything in the library to do this, so I tried to
create it with a Mutex and Condition Variable, like:
class Token {
private Mutex mut;
private Condition cond;
private bool completed;
private int retCode;
this() {
mut = new Mutex;
cond = new Condition(mut);
completed = false;
}
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;
}
}
But I am getting totally tripped up by the 'shared' qualifier.
All objects of type Token are meant to be shared, and yet when I
try to define them as such, I get complaints that I can't
implicitly convert Mutex and Condition objects to shared. When I
do so manually, then I get errors that the Mutex and Condition
member functions can't be called on shared objects.
That seems odd. Aren't Mutex objects and Condition Variables
explicitly created to be shared across threads?
I assume that I am mixing older, low-level libraries with newer,
higher-level libraries, but I can't figure out what the library
requires and what the language/compiler is enforcing.
Any help would be appreciated.