I need to sync data with WebDAV and other similar backend that do not have a concept of atomic operations. In some cases, I might need to read a file, change its content and write it back. And of course, I want to make sure that no other client read and write the same file in the meantime.

I'm wondering if there's any general technique to handle this?

I thought about having a lock file and if that file is present, no other client can write to the backend. The problem is that there could also be race conditions when creating the lock file: first I need to check if it exists and, if it doesn't, create it. But of course, another client might be doing the same operatons at the same time.

So any idea how this can be handled in general? (It doesn't have to language or OS specific as the backend could be anything, like ftp, scp, etc.)

  • $\begingroup$ If the number of working threads is not too much, you could try implementing Peterson's Algorithm. Though, it inherently does NOT guarantee Bounded waiting for a multi-process scenario. $\endgroup$ – RandomPerfectHashFunction Oct 11 '19 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.