Rono
Rono

Reputation: 153

File Locking in C++ For simultaneous Read and Write Lock

How can i lock a file for read and write operation. That is If "ABC" file name is in Write lock, it also provide Read Lock on the same locked file. In normal case we want to wait till write operation completion.So if there any ways to acquire this kind of locking

Upvotes: 4

Views: 4092

Answers (2)

Felix Glas
Felix Glas

Reputation: 15524

Many programs simply use a lock file to signify that a certain file is currently in use for writing.

The lock file is later removed when done writing.

For example, when process #1 is about to start writing to file example, it creates file example.lock. Later when done writing, it simply removes example.lock.

When process #2 want to read from file example it first checks if file example.lock exists. If it does then the file is locked for write operations and process #2 will have to wait.

Upvotes: 3

piokuc
piokuc

Reputation: 26204

shared_mutex from Boost implements read/write locking.

Upvotes: 0

Related Questions