Hacker News new | ask | show | jobs
by zogrodea 701 days ago
My experience with this is in .NET, which has methods like readFile (which is async) and readFileSync.

.NET doesn't really need to provide two separate utility methods like this though, because you can use Task.wait to block until the async task is done.

1 comments

File.ReadAllBytes/Text/Lines use synchronous underlying file API.

Their async variants call different OS APIs for asynchronous operation where available (Overlapped IO on Windows, on Linux it is specially scheduled (p)read and write calls).

A similar distinction applies to Socket where asynchronous calls use an internal epoll-based engine while synchronous ones are plain send/recv.

Generally speaking, in synchronous code there is no advantage to calling `.Wait()` or `.GetAwaiter().GetResult()` over synchronous APIs when such are offered, and if you can help it, it is best to update the code using them to be async too if your application is multi-threaded. Luckily it's quite easy in most situations unlike what HN public (that hates better languages like C#) would lead you to believe. But ff you do have to do block on waiting for a task, the impact on throughput is usually negligible if you do so within say threadpool worker - the implementation nowadays can cope with this very well and is more resilient to scenarios that used to be problematic.