Concurrent block in hacklang

456 views Asked by At

Since hack is a single threaded language, what is the benefit of using a concurrent block?

concurrent {
   await func_a;
   await func_b;
}

My understanding is that one job is waiting until the other job is over.

1

There are 1 answers

0
Boaz On

Concurrent doesn't mean multithreading

The concurrent block will wait for all async operations (awaitables) in that block similarly to javascript Promise.all (also single threaded).

Without concurrent:

await func_a; // 1 sec
await func_b; // 2 sec
await func_c; // 3 sec

// will get here after at least 6 seconds (sum of requests time)

With concurrent:

concurrent {
   await func_a; // 1 sec
   await func_b; // 2 sec
   await func_c; // 3 sec
}
// will get here after at least 3 seconds (longest request time)

It fits if you want to make multiple IO requests in parallel.
It doesn't fit if you want to run multiple CPU jobs.