4 ms·
> That’s why C# has that little caveat. You can avoid the pain of async in C# by using threads. This is the first time I've seen threads referred to as a way t
by thaumasiotes 6y ago
> That’s why C# has that little caveat. You can avoid the pain of async in C# by using threads.
This is the first time I've seen threads referred to as a way to avoid pain.
- taffer 6y agoThreads are fine as long as they do not share mutable data structures. For example, multithreaded application servers are a rather painless experience.
- mumblemumble 6y agoThat observation doesn't really contribute to a comparison of dedicated threads and async/await, though, because the same is true of async routines.
- eyelidlessness 6y agoSince all of these concurrency approaches’ pain points are around shared mutable state, maybe it’s shared mutable state that’s the pain point.
- bglusman 6y agoYup, this is the conclusion of Joe Armstrong and why Erlang/Elixir/BEAM just doesn't allow mutable state, other than process dictionary which is inherently non-shared.
- deleted 6y ago[deleted]
- ancarda 6y agoI honestly prefer threads and mutex locks to async/async. Feel free to burn me at the stake.
- formerly_proven 6y agoWhether async or threads are preferable depends a lot on context and often the answer will be "both". E.g. GUI code -> async, because you must do everything on the UI thread. Heavy lifting in a GUI app -> threads. Traditionally you'd do this with a huge mess of callbacks, but these days it's reasonably easy to just write the GUI portion using async and have background computations run in threads signal their results through futures into the async code, which makes everything so much easier.
- tasogare 6y agoThey aren’t even solving the same problem. Threads exist to use multiple CPUs while async/await are here not to waste time waiting on I/O.
- coder543 6y agoThreads existed long before multi-CPU computers or multi-core CPUs were common, so no... they don’t exist for that reason. Parallelism and concurrency are orthogonal concerns. Multiple threads can run on the same core, while concurrent awaits can run on separate cores (depending on the language). Or the opposite! Threads definitely solve wasted time waiting on I/O, but OS-level threads are a very limited resource in many environments, which can cause problems more quickly in programs with unbounded concurrency, and they tend to have a lot of startup cost, so they have to be created judiciously to avoid hurting performance. Languages like Go and Elixir give the programmer access to unlimited lightweight “threads”, and that works great for concurrency, without requiring developers to re-color their functions. One additional note is that some OSes (including Linux) have struggled to support true nonblocking File I/O for many years, so you literally have had to use multiple OS threads for I/O concurrency... one for each file operation in progress. Otherwise you would block the async executor! Languages often handle this implementation detail for the programmer, so you just don’t realize you’re using a thread-per-await sometimes. (io_uring seems to finally solve this in a meaningful way on Linux)
- tzs 6y agoSame here. I'm curious how you got there. I think a large part of that for me is because early in my career I got involved in Unix kernel work, first writing device drivers and later doing Unix ports, including both porting a swapping Unix to paged hardware (and rewriting pretty much all process and memory management from scratch) and porting a paging Unix to swapping hardware. That was followed by a few years largely doing embedded stuff on systems too small to have operating systems or memory management units. For those I'd write a small kernel that supported threads and scheduling (sometimes cooperative and sometimes preemptive) and then write the embedded applications much like I would have written them on Unix, using threads instead of processes. So for me threads are easy and things like async/wait seem overly complicated and less clear because I came at concurrency from the operating system side of things and the hardware side of things (e.g., dealing with interrupts) where thread-like thinking is natural and perhaps required. Maybe it is conceptually harder for people who come at it from the application side of things?
- mumblemumble 6y agoFor a long time I experienced excessive pain with both threads and task parallelism. It took a good year or two to realize the problem was that I didn't have a good instinct for figuring out which technology is more appropriate for a given situation.