Summary: There is a reader thread and a writer thread accessing same memory space without synchronization. Is there any run-time error( NOT logical error ) or a risk of breaking the process or threads?
I’m trying to make a simple task scheduler.
There are some worker threads and they have their own task queue.
The task scheduler push tasks to the workers’ queue.
I want the scheduler know the least busy thread, whose queue is shortest.
So I need some shared integer variables to store each queue’s length.
Each worker thread writes the length of its own queue on specific variable.
And the scheduler reads that variables to know the shortest one.
So each variable has one read and one writer.
This is R-W problem and i need a mutex.
But i don’t want any overhead and know the exact length of queues.
So I want to let the threads access the shared values without synchronization.
Is there any problem, without the inaccurate value?
There’s no law that says, for all platforms, all languages, and all standards what must happen when two threads access the same memory space without synchronization. Some platforms may allow it and add the synchronization themselves. That’s not impossible. Some may test for it in all cases and guarantee that the process will crash. That’s not impossible either.
As generic as this question is, the answer could be anything. You might as well ask “What happens if I do something I’m not supposed to do?”
POSIX, for example, allows the process to crash. Win32, for aligned 32-bit accesses, requires it to, at worst, give stale values. There’s no universal law.