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:
- Two (or more) execution contexts (EC*) need to concurrently access shared storage,
- Those ECs need to write to that shared storage.
- 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:
- If no writes occur, there is no race condition.
- If the writes are not to a shared location, there is no race condition.
- If the values written are not derived from a value held in shared storage, there is no race condition.
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.
|
---|
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 |