4 ms·
Keep it simple. Have global state where it needs to be global. The easiest way to avoid problems with multithreaded writes to globals is to not have multithrea
by AngriestLettuce 4y ago
Keep it simple. Have global state where it needs to be global.
The easiest way to avoid problems with multithreaded writes to globals is to not have multithreaded writes to globals. Ensure all writes to said global only occur in one thread. One possibility is to use e.g. locks and semaphores to ensure only one thread writes at a time, but nothing can beat the amazing synchronity that is imperative programming. Try it out some time. Tell your friends.
The real secret here is that there are very few things that actually need to be globally writable. Most things that are global should be constant, so not a problem. For those which aren't, you probably have some kind of IPC message passing thing in your programming tool of choice. Use it to pass any state changes to global state to your main thread, which changes it. Be aware that race conditions may happen. If they cause you issues, make them not happen any more. Don't worry so much about life.
There will be problems with every approach. Pick one with few problems, and deal with them as they come. Multithreaded programming is inherently complex. No need to make it worse than it is.
- parf02 4y agoI'm curious how you are doing multithreaded in the front-end. You're talking about modifying your global state from a web worker?
- AngriestLettuce 4y agoA front end is simply an application that talks to a different application to receive its data instead of processing it directly. It can be as threaded as you like.