http://www.perlmonks.org?node_id=1005175


in reply to Re^2: Thread safe equivalent of LINUX touch command
in thread Thread safe equivalent of LINUX touch command

I guess I think of "?-save" as that you don't allow a race condition to exist, and must use locking to prevent the race condition.

In order for the potential for a "race condition" to exist, several algorithmic prerequisites are needed:

  1. Two (or more) execution contexts (EC*) need to concurrently access shared storage,
  2. Those ECs need to write to that shared storage.
  3. The values they write need to be derived from a value previously read from (the same or different) shared storage.

These algorithmic prerequisites are required because:

As (the normal meaning of) 'touching a file' does not require any of these, a "race condition" cannot occur.

That said, it is possible to contrive a bespoke "touch" requirement that could introduce a race condition. For example, an algorithm might require that not just the 'modified' timestamp be set; but also, one, the other, or both of the other two timestamps be set to the same value.

In the case, if the OS, or the tool, required multiple calls to set multiple timestamps, it would be possible for one thread to modify one of the TSs, then get swapped out; then a second thread modifies all of the affected TSs; and then the first thread gets another timeslice and modifies the other one or two TSs.

The result would be that the file would get a mismatched set of timestamps.

The windows SetFileTime system call (can) set all three in one call, thus preventing that possibility, but that does not prevent a particular implementation of 'touch' from choosing to call the function multiple times to set multiple timestamps, in which case a race condition would be introduced.

And I don't know if other OSs can update multiple timestamps as an atomic operation or not.

Hence, to really answer the OPs question, it will be necessary for him to answer my question and clarify exactly what he is doing with his 'touch' operation.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

The values they write need to be derived from a value previously read from (the same or different) shared storage.
  • Comment on Re^3: Thread safe equivalent of LINUX touch command

Replies are listed 'Best First'.
Re^4: Thread safe equivalent of LINUX touch command
by zentara (Archbishop) on Nov 23, 2012 at 14:15 UTC