Multithreading With A Global Variable: If Only One Thread Is Changing The Variable, Is It Necessary To Lock It?
As titled, several thread accessing one variable, and only one thread will change the variable, and all the others will just read its value. Like this: Thread 1: while True: a
Solution 1:
Generally speaking, how catastrophic a problem it would be would depend on what the consequences were of one of the other threads obtaining the wrong value.
The answer may indeed be computer-language related: See the Software section in the general Wikipedia article on Race condition.
Related to that is the fact that, thanks to the "GIL", for the most part Python programs don't do real multithreading since the interpreter isn't thread-safe.
Solution 2:
Irrespective of the language, updating a variable by two separate threads will affect the value. But printing the values will not. The only issue is all the threads (2 onwards) will print different values.
Think this is happening in a database. One person can update the dB, but many can read it.
Post a Comment for "Multithreading With A Global Variable: If Only One Thread Is Changing The Variable, Is It Necessary To Lock It?"