Home > Back-end > Only one reader thread, a writer threads, using read_idx and write_idx, whether can replace the lock
Only one reader thread, a writer threads, using read_idx and write_idx, whether can replace the lock
Time:09-24
Apply a piece of memory, 10 small pieces and recycled Write threads first, and then read a thread to read data, using a lock can easily complete the mutex,
If I don't use locks, fully use the int read_idx=0, int write_idx=0, seem to be able to completely replace the lock, please compare the advantages and disadvantages of this way and lock? Here is to use read_idx, write_idx pseudo code,
write_thread () { While (1) { If (write_idx!={read_idx) Memory [write_idx]=random_value; } the else continue;
If ((write_idx + 1)==10) Write_idx=0; The else Write_idx + +; } }
Read_thread () { While (1) { If (read_idx!={read_idx) Printf (memory [read_idx]); } the else continue;
If ((read_idx + 1)==10) Read_idx=0; The else Read_idx + +; } }
CodePudding user response:
Honestly lock
CodePudding user response:
Depends on whether the two threads can share data, look from your routine, not a substitute for locks, Add lock or advice
CodePudding user response:
In addition, this code is there to be the problem?
if (read_idx! Read_idx)={
CodePudding user response:
Lock it, a simple example, the code
If (write_idx!={read_idx) Memory [write_idx]=random_value; } the else
Judgment even when the index range, but the judgment, the assignment before another thread operation, lead to connect with the same index, that behind will be the result you want? Judgment to the assignment is not atomic operations
CodePudding user response:
You after the judgment of value, before the next code execution, you judge the value of it is possible in another thread is modified,
CodePudding user response:
If the order is order to read and write is no chains, need to use variables show that can read and write location
CodePudding user response:
I'm sorry, your step is not atomic steps, errors,
CodePudding user response:
Bosses say all upstairs, mainly judgment equal and assignment is not atomic operation, there is no guarantee that the index does not change
CodePudding user response:
You this kind of method and the lock point is the same, just have such may, after you write just judge index, the reading thread at work, too, have changed the value of the index
CodePudding user response:
If ((read_idx + 1)==10) Read_idx=0; Here is the hole, if finished read_idx cycle, the write has yet to start, you is a tragedy, There is you it is double thread, also only suitable for double thread, thread if it is 3 or more?
CodePudding user response:
If (write_idx!=read_idx) If here have no beginning, never start,
CodePudding user response:
The lock is the way
CodePudding user response:
You can say that, there is no advantage
CodePudding user response:
Don't lock, memory access conflict
CodePudding user response:
If read threads have operations, resulting in two index is equal, in addition to read threads will not read the memory, so feel there is no problem,