so I have a boolean type in C ++ on a multiprocessor machine. A variable begins with life as truth, and then there are several flows, any of which can spell it as false.
At the same time, these threads can also read this variable to check its state. I donβt care if the reading of this variable is synchronized with any of the records, each of them occurs in different places in the code, and it does not matter whether it comes before or after any particular record. Now I need a lock for this boolean?
The only way I need a lock is if at a very low level the memory can be damaged by two competing entries. If, for example, the assembly instruction on processor A writes 0 to a byte that represents a logical value, while processor B does the same ... and instead of writing 0, the memory ends with a value of 22 or something. It might ruin something.
So, in general, if proc A writes 3 to a memory location, and proc B writes 7 without synchronization, I am sure that in the end you will get at least 3 or 7? Or is it easy to break memory?
Edit:
Thanks for the comments guys. Additional information: in the program, of course, there is synchronization. To summarize, the corresponding flag indicates that a specific memory pool is "dirty" (it is necessary to compact). Any thread can therefore decide to set this flag to false (this means the pool is dirty). For example, freeing memory from a pool makes it dirty. Any thread can also read this flag and set another flag to signal the need for cleaning - this check is performed when the memory is allocated from the pool, cleaning is signaled if we have little memory. Somewhere in my main critical section between iterations, where each thread goes to look for more data to process, I will have threads checking this second flag, and do something suitable to make sure that: all other add-ons finish their current iteration , one thread clears the memory, sets the first flag back to true (as it is not dirty in the pool), returns the second flag to false, and then frees all threads again.
Therefore, I do not think that I need a lock, because: a lock ensures that the record does not happen at the same time as another record or reading. But who cares until the hardware fails me, the worst case scenario is that reading randomly before or after writing is the same as if I protected it with a lock, then we would really be sure that this happened before or after ...
I think the same argument applies to the second character mentioned above.