Fix your damned data races
blog.mozilla.org
blog.mozilla.org
The sign says "You must be at least this tall to write threaded code", and it's been placed 8 feet off the floor, next to a well respected senior developer who must constantly write threaded code (and who, when standing, is a good two feet shy of the sign).
Threading properly is hard, and yet it's becoming more and more integrated into our lives, from our browser to our backend interpreters like Node...
Concurrency needs a Rust-like language to protect us at compile time against data races. The restrictions it would impose on what we could do might be abhorrent to our generation of coders, but the benefits would be far reaching.
The problem occurs in that both programmers and our tools are terrible at guessing when such order dependencies will occur, except when working with "embarrassingly parallel" tasks.
The default should be message passing concurrency, as Go advocates -- which of course can be done with simple threads and queues. Unfortunately in C++ that leads to tricky memory management issues.
That said, you can still have a data race with "immutable" data at the thread level as well.
For example, we have a system which has A processing threads, whose results are collated by the master and passed to a set of B processing threads. All of these threads use message passing and an immutable view of the master's environment which determines their behavior.
A command comes in which changes the master's environment, and the master sends the A and B threads a "die" command and waits for the flow of data out of the threads to stop. It then spins up new A threads and B threads to process the data with the new environment.
The data race appears when some old A threads become blocked on some external resource or are just unlucky in scheduling, passing data based off of outdated assumptions to the new B processors will be attempting to process data from the original A threads which have the wrong view of data.
The solution in this case is obvious - set up new queues, or more authoritatively kill off old threads, or change a parameter in the messages being passed, or pass the entire state from thread to thread, or... but the solutions to the problems outlined in the OP are also obvious, once the race condition is identified.
The problems you see with memory races (e.g. publishing a pointer to a structure which does not have all its construction memory stores retired) are at a different level of strangeness, however.
Though it is admittedly a fair bit more sane for most applications.
It still isn't "easy", but at least it's back in the realm of feasible.
Message passing is another way of dealing with the 'shared,' too. But especially with the new RFC that just landed, Rust has some pretty amazing capabilities in this space that should make it incredibly useful for concurrency. And because it's based in the type system, not the language itself, you can take advantage of that safety and write your own concurrency abstraction with the same guarantees, esentially for free.
Not sure if this is what you meant, but Rust does protect against data races. You are unable to share data unless you use message passing, share read-only data, use atomics, or use mutexes.
But, I don't know if "use FP if you want parallelism" is an acceptable solution currently. My intuition is that latency sensitive application can't use these languages because they don't expose the low-level primitives (read pointers) that make the difference in such applications. But I haven't written and profiled enough FP code to know if this is absolutely the case.
I thought separate functions weren't allowed to have dependencies like this?
Once your code is in a monad, every one of those problems may be back (including memory race).
STM is actually really simple in Haskell, and implemented mostly as a library(!). Have a look:
http://computationalthoughts.blogspot.nl/2008/03/some-exampl...
MVars can deadlock, but you can't accidentally mutate/read a value without taking the lock (unless you use IORefs in conjunction with MVars).
STM handles races by detecting them and re-starting a transaction if a race occurs, so races aren't possible. (Accessing IORefs is disallowed by the type system inside an STM transaction, so you're safe there.) STM also isn't susceptible to deadlocks, so it's generally the safest option if you need non-deterministic threading, and invoking transactions from the IO monad fits with the structure of your program.
There's also par/seq, strategies, and the Par monad which are great for parallel computation and aren't prone to races or deadlocks. They can be called from anywhere in a Haskell program and are deterministic -- they're guaranteed to always return the same result when given the same inputs. Results cannot depend on the order in which threads are scheduled.
tl;dr: don't use IORefs and you should be fine
http://www.gnu.org/software/libc/manual/html_node/Atomic-Dat...