-2

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.

2 Answers 2

1

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
  • but... then you have to check that the result is ready somewhere. or ::get() blocks. What am I missing? Mar 21, 2013 at 22:54
  • @LorenzoPistone If you need the result in your computation right now, then ::get() blocking isn't an issue - you need to wait for that info to be available to continue your computation. If you don't need it right now, then why are you calling ::get() at all?
    – Tacroy
    Mar 21, 2013 at 23:04
  • @LorenzoPistone await/get() blocks, so you only call them when you absolutely need the result. Isn't that what you want? Seems that way to me with your example at the end. Mar 21, 2013 at 23:07
  • no, it's not what I want. I want something that jumps out of the function, and returns there only if the result can be fetched. Something like "automatic" lua coroutines. Mar 22, 2013 at 0:19
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.