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,

The
reference reply: 4th floor hero @ extremity
lock, 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:

As long as the judge when there is no problem, other times change, will not be affected, the statement should not be interrupted by half, please illustrate the problem where, thank you,

reference 5 floor wallesyoyo reply:
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:

references 9 f cocoabird 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 is running, just changed the value index


After judgment when you write the index, reader thread even if change the index value, also won't affect, because reading will continue to determine whether and when writing index are equal, will go to read the data, you can, for example say me the index above process problems in where? thank you

CodePudding user response:

not

CodePudding user response:

Under the reference implementation of c + + 11 atomic variable spin lock

CodePudding user response:

Can't, you consider not enough fine and have a good look at your code, there will be conflicts

CodePudding user response:

I see along while, these people are not read the code? A thread to control the write pointer, a thread control read pointer, why there will be conflicts? Is a can't, not atomic operations, is about why don't you can?

CodePudding user response:

Write pointer wrote half was read pointer, so wrong!

CodePudding user response:

Only need to put the write_idx, read_idx defined as atomic can, also do not need to use lock,

The original code, there is a place to have the wrong number, reading the thread, the statement is
 if (read_idx! Write_idx)={


Because the post editing function is not available, we corrected,
 
Atomic_int read_idx=0;
Atomic_int write_id=0;
Assuming that the write step to start early, write_idx into 1 first, startup will not deadlock,
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!={write_idx)
Printf (memory [read_idx]);
} the else
continue;

If ((read_idx + 1)==10)
Read_idx=0;
The else
Read_idx + +;
}
}


references 9 f cocoabird 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 is running, just changed the value index

CodePudding user response:

reference 23 f registercsdn response:
just put write_idx, read_idx defined as atomic can, also do not need to use lock,

The original code, there is a place to have the wrong number, reading the thread, the statement is
 if (read_idx! Write_idx)={


Because the post editing function is not available, we corrected,
 
Atomic_int read_idx=0;
Atomic_int write_id=0;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related