Junior — Middle
What difficulties may arise if all threads work with a shared global variable?
sobes.tech AI
Answer from AI
If multiple threads work with a shared global variable without synchronization, the following problems can occur:
- Data races — when two threads read and write to the variable simultaneously, resulting in unpredictable outcomes.
- Unpredictable program behavior — due to data inconsistency.
- Visibility issues — one thread may not see updates made by another.
- Errors and crashes — for example, data structure corruption.
To prevent these problems, synchronization mechanisms are used: locks (mutex), atomic operations, message queues, or other access control tools.