3 ms·
For the example in question one approach which might work well is to realize the test is superfluous. Just open the file with the right flags. That is, the fla
by magicalhippo 3y ago
For the example in question one approach which might work well is to realize the test is superfluous. Just open the file with the right flags.
That is, the flag for create if not already existing and flaf for deny sharing so any other process/thread will fail to concurrently open it.
Then you just handle the case when the open fails.
Of course this relies on those flags being available and working. For example shared files might not respect the sharing lock.
Many cases of avoiding race conditions boil down to something similar: avoid the problem by not doing extra work.