I am designing an application that has some scripting capabilities: the host application may call the scripts to perform some operations when some events occur. To keep it easy and newbie-friendly, there is a single thread for everything: this means that if the script calls something blocking, the entire application hangs. So, I either use callbacks or polling to wait for a result of a blocking call to be ready. What is ugly in this approach, is that a function that happens to call a blocking function, has to be split in parts. For polling, I’d have
function do something part 1
blabla
launch something lengthy
schedule do something part 2
end
function do something part 2
if result not ready then schedule do something part 2
use result
blabla
end
Callbacks are only slightly better:
function do something
blabla
launch something lengthy, when done call callback
end
function callback
use result
blabla
end
If one has several blocking calls, and each one depends on the result of the previous, code gets uglier and uglier. What I’m looking for is for some programming language that have some builtin support for this pattern. Something like
function do something
blabla
launch something lengthy
when done: //fictional keyword
use result
blabla
.... //more and more blocking calls
end
Of course if there are better solutions I’m glad to listen.
What I’m looking for is for some programming language that have some builtin support for this pattern.
Don’t they all have them by now? 😉
I know of two, they all work the same: launch a function in parallel, possibly on a different thread, and get the result later:
C# async
var task = BlockingFunction(arg);
// ...later...
var result = await task;
C++ std::async
auto handle = std::async(std::launch::async, blocking_function, arg);
// ...later...
auto result = handle.get();
4
Java has something called a CountDownLatch, which should do what you’re after. From the JavaDoc for it
class Driver { // ...
void main() throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
}
}
class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}
public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {} // return;
}
void doWork() { ... }
}
The basic idea is you can configure the Latch to stop progressing until its counted down to 0. Once it hits 0, the waiting threads continue.