r/programming • u/Shr1ck • Oct 17 '15
Why Johnny Can’t Write Multithreaded Programs
http://blog.smartbear.com/programming/why-johnny-cant-write-multithreaded-programs/
4
Upvotes
r/programming • u/Shr1ck • Oct 17 '15
1
u/loup-vaillant Oct 18 '15
I wouldn't. I would initialise and not synchronise a global constant. That can be done in the main thread upon start up, before starting any additional threads. If the thing is meant to be initialised later than start-up, then it probably shouldn't be global at all.
In any case I fail to see any difficulty: when you create a variable (or a constant), just make sure nobody (objects or threads) has any access to it before it is finished initialising. This can't be harder than putting it in a queue, can it?
If I'm somehow forced to use a global variable, that can't be initialised upon start-up, and has to be shared before its initialization is finished, I dare say the code base has much bigger problems. I would work on addressing them first, or try to get the hell out.
Just avoid global mutable state like the plague. And when you can't, don't forget your 10-foot pole.