Thread-safe analysis of circulating buffer

In general, we want to ensure thread safety when using a loop buffer, will represent the write flags and signs reading with a r a w and go.

In the case of an unlocked, consider writing a thread, multiple threads read, this time, did not use a memory storage unit to identify the full results of this model can indeed thread-safe reading. At most, only a small number of data each read.

However, in the case of multiple threads, if there are multiple write, multiple read, the situation is very complicated, this model simply could not hold, because we can not guarantee write ordering data, will make multiple threads writing will become a mess, this time can not be counted thread safe. This really ensures that each data can be written into it, but the situation will lose the data exists, a w has not been updated, the new operation went w old w, this memory will be overwritten. Unsafe! ! ! ! Therefore, in order to ensure the case of multiple threads to write, or should be added to the thread of protection mechanisms to write.

Guess you like

Origin www.cnblogs.com/lison-record/p/12145275.html