1

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.

Amir
  • 307
  • 3
  • 14
  • 1
    I recommend Hack docs' own [Introduction to async operations](https://docs.hhvm.com/hack/asynchronous-operations/introduction) which covers how concurrency is possible in a single thread, which I believe is your underlying question. To summarize, the jobs are executed _concurrently_, not one after another. The gist is that the "leaf" Awaitables of the async tree entirely come from waiting on I/O (e.g. MySQL) or on a timer. When all Awaitables are awaiting, the internal Hack scheduler has control and is just waiting for one of those sources to fire, which it can do in a single thread. – concat May 09 '22 at 02:34
  • thanks. was looking for that link, but was not able to find it. – Amir May 22 '22 at 16:03

1 Answers1

2

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.

Boaz
  • 1,212
  • 11
  • 25