There is system with few thread
Thread#1 : Writer , priority:0
Thread#2: Notifier.. Write completed, priority:0 (DSR Context)
Thread#3: flush data if some data in memory (different then storage), priority: 1
Thread#4: read data from X,Y,Z and write on P, priority:1
Here is a scenario …
Thread#4 access data D1, Same time thread#1 comes and since it is high priority, it gets the access to D1, once it completes Thread#3 scheduled in and checks that D1 is different then storage copy, so it writes the to storage. Now when thread#4 comes there is nothing to do for it :-(.
for some reason if you want other way to be done.. without changing priority or anything else as that may affect other things(not whole picture is described) etc..
you have to follow insane way :p( plz read https://kunalkushwaha.wordpress.com/2007/07/24/science-vs-insanity )