An imaginary lock mechanism.

Here comes our scenario. Simply, Bob and Alice are a write and a reader. Bob writes things and Alice reads it. Rules are:

1) Bob can write no matter Alice is reading or not. Writing is block-free.
2) When Bob is writing, Alice cannot read.
3) When Alice finishes reading, she can know if Bob writes during her read-data-time.

2) and 3) are actually one rule, but I list two for good discussion. The problem can be solved by one mutex and one counter(version number), I knew. Which I do not know is, is the problem a well-know scenario named by terms? Does anyone study at it or I am just making a wheel?
Last edited on
Its a real problem, yes. Techniques vary across OS and by the needs of the tools, but reading a partially written file can be bad news for some programs esp structured files like xml being fed to a parser with only 1/2 the file... Some (most??) os allow the opener to tell the os to prevent readers. Some don't and you have to use blocker files or something like that for your mutex. Inside the same program, its easier of course.



That seems to be worded contradictory: if Alice is already reading and Bob starts writing (per #1, he can do that at any moment), why (and how) would Alice stop reading and block to satisfy #2?
There are plenty of ways to share data between a single writer and one or more readers using a lock for both and plenty more lock-free for both.
on second thought, this does sound very close to the description of how https://en.wikipedia.org/wiki/Seqlock or stamped lock operates (I think closest to seqlock, except #2 doesn't happen there)
Last edited on
Thank you Cubbi, Seqlock is the answer.
Topic archived. No new replies allowed.