logoalt Hacker News

tossandthrowyesterday at 9:15 PM3 repliesview on HN

The author does not seem to have made any non-trivial projects with asynchronicity.

All the pitfalls of concurrency are there - in particular when executing non-idempotent functions multiple times before previous executions finish, then you need mutexes!


Replies

laserbeamtoday at 1:37 AM

Mutexes are part of the Io interface in zig. So is sleep, select, network calls, file io, cancellation…

ajrossyesterday at 9:38 PM

> All the pitfalls of concurrency are there [in async APIs]

This is one of those "in practice, theory and practice are different" situations.

There is nothing in the async world that looks like a parallel race condition. Code runs to completion until it deterministically yields, 100% of the time, even if the location of those yields may be difficult to puzzle out.

And so anyone who's ever had to debug and reason about a parallel race condition is basically laughing at that statement. It's just not the same.

show 2 replies
alerighiyesterday at 11:03 PM

You don't need mutex in async code, since there is no parallel execution whatsoever. In fact languages that use async programming as a first class citizen (JavaScript) don't even have a construct to do them.

If you need to synchronize stuff in the program you can use normal plain variables, since it's guaranteed that your task will be never interrupted till you give control back to the scheduler by performing an await operation.

In a way, async code can be used to implement mutex (or something similar) themself: it's a technique that I use often in JavaScript, to implement stuff that works like a mutex or a semaphores with just promises to syncronize stuff (e.g. you want to be sure that a function that itself does async operations inside is not interrupted, it's possible to do so with promises and normal JS variables).

show 3 replies