What to do instead of async/await in a systems language

Viewed 81

I have a concrete problem that in a higher level language I would solve using async/await: we have a blocking system/hardware communication/network call that takes several seconds to complete. We would like to have that happen in the background while we do some other of such calls in parallel.

I have thought of a couple of solutions and there might be better ones than these:

  1. start a thread and signal condition variable/semaphore once it's done;

  2. provide a callback that is executed when the call finishes (old JavaScript style);

  3. create your own custom scheduler to actually mimic async/await.

What's the ideal solution to this in a systems language such as Odin or C?

1 Answers

I would recommend using whatever asynchronous approach is most common in your system / language. I do not recommend using a separate thread, and I do not recommend trying to port a high-level style of asynchronous programming into a lower-level language / platform. You want your consumers using something that feels natural to them, rather than learning a whole new paradigm just to call your API.

If you're on Windows, you should be able to signal a ManualResetEvent on completion. Explicit callbacks would also be acceptable.

I haven't written asynchronous code on Linux, but I suspect adopting libevent or libuv would be the way to go.

If you're exposing an API for others to consume and you want it feeling the most platform-like, I believe you'd have to do that at the driver level. That allows you to fully implement support OVERLAPPED (on Windows) or epoll (on Linux).

Related