Re:Are Go maps sensitive to data races?
by binarycrusader on Monday December 07, 2015 @05:46PM
(#10693377)
Well, they believed it was worth adding something (from a commit today):
https://github.com/golang/go/commit/50c5042047be3af36e7bb478...
Re:Are Go maps sensitive to data races?
by alkonaut on Tuesday December 08, 2015 @08:30AM
(#10696189)
That's interesting. Would have liked seeing something like that in the Java and .NET equivalents.
The check is actually very cheap -- you can check the source yourself -- but as you might expect it won't catch all unsafe use.
Re:Are Go maps sensitive to data races?
by alkonaut on Monday December 07, 2015 @07:07AM
(#10688890)
Like you suggest you can make a cheap check that sometimes triggers an exception. Or an expensive check that always triggers on all concurrent access. As far as I can see in the java HashMap source, the concurrent modification check is actually only done for the enumerator (?) so the problem of multiple concurrent insertions, or even reading-while-inserting will never be caught anyway. Just like Go, or .NET.
A check for single thread access (which is even stricter than non-concurrent access which allows several threads as long as they aren't used concurrently) would be pretty cheap: store the thread ID on creation and then verify on reads and writes.
Failing on concurrent access otherwise on all reads and writes would basically mean that you add a lock to the write operation, and fail any reads while anyone is writing. This however is close enough to making it a full concurrent map, so it isn't worth it.
The whole reason there are two implementations of the dictionary (Concurrent and Non concurrent) is that the performance cost of the concurrent implementation is too high to carry for the non-concurrent one. Depending on the cost of checking for concurrent access, a lot of the gain of using a simpler non-concurrent one could be lost.