Hacker News new | ask | show | jobs
by aw1621107 51 days ago
> You can do it all with library functions that just take regular int* (and so on).

It's not just about atomics, but you might still find Hans Boehm's 2005 paper "Threads Cannot Be Implemented As a Library" [0] interesting. It describes (among other things) correctness and performance issues that can arise if the compiler and/or language don't know about thread/atomic semantics.

[0]: https://web.stanford.edu/class/archive/cs/cs240/cs240.1236/o...

2 comments

Yeah, it's all fun and games until you realize that you compiler may invent writes in order to eliminate a branch. Which is rather nonsensical, when you stop to think about it — surely an additional store to global can't be faster than a mispredicted branch?
Branch mispredictions on deep machines can be devastating, whereas a store can hit in your L1. Storing can definitely be faster than a mispredicted branch if you think you'll hit in your L1/L2
store to a global is typically 0 cycles (although subsequent reads may be slowed down)
It seems like the guy analyzes how Pthreads are formally defined and declares that "Threads cannot be implemented as a library". Maybe if the title was changed to "Pthreads cannot be implemented as a library" it'd be true. Pthreads are fucking abysmal.

> We explicitly do not address the less frequently used environments in which multiple concurrent threads communicate communicate primarily through message passing, such as C programs communicating only through the MPI[34] library, or Erlang[11] or Concurrent ML[28] programs.

Heh, there's your answer. CSP FTW!

Nothing to do with pthreads. As long as you want to use shared memory to communicate with concurrent processes, you need a memory model with compiler support. Even if you exclusively use mutexes.
> As long as you want to use shared memory to communicate with concurrent processes

That's something pthreads do and CSP doesn't.

> you need a memory model with compiler support. Even if you exclusively use mutexes.

If you use CSP you can effectively eliminate shared memory in user space and you don't have to handle this with the compiler. Then you can say "here's a way to communicate between processes in user space, the runtime/kernel handles the shit underneath. If you want to do it some other way we can't guarantee it'll work."

For better or worse, our current hardware architectures present a shared memory interface for intercore communication. So if you want a non-toy CSP programing language you have to implement with a programming language that support shared memory concurrency (for the OS at the very least).

And of course any sufficiently complicated message passing program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of shared memory and coherency[1].

[1] Yes, the reverse is also true.

Not sure what CSP is supposed to mean, but if you eliminate shared memory and have the kernel copy data between threads, you're leaving a ton of performance on the table.
> Not sure what CSP is supposed to mean

https://en.wikipedia.org/wiki/Communicating_sequential_proce...

> but if you eliminate shared memory and have the kernel copy data between threads, you're leaving a ton of performance on the table.

If that's your bottleneck then you've either already solved every other class of performance issue with your program or you're working in a fantasy environment-- you're probably not hacking on FFmpeg right now. 90% of performance is I/O bound, whether it be the disk, the network or whatever. You can easily ascertain the correctness of a CSP system, because it's just message passing and inherently synchronous. Not so much with shared memory. It's a tradeoff. Unix pipes are also pretty goddamn fast, like, this is an issue people have spent time squeezing as much performance as possible out of. Google uses Go for all of their important backend shit. The entire internet is a giant message passing system.

> 90% of performance is I/O bound, whether it be the disk, the network or whatever

funny you say that, because to take advantage of the monstrous bandwidth offered by modern ethernet, OS bypass with the card DMAing directly in your process cache is a must. That reintroduces all the "fun" shared memory ordering issues, except trickier (you are talking with a PCI device, not just another core) and buggier.

Re disk, I'm sure someone will revive optane at some point, and we will be back with memory model issues in our disk I/O path.

The reality is that a programming language that claims to be usable for system programming must be able to deal with shared memory and provide a concurrent memory model.