Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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

like image 941
Rono Avatar asked Sep 02 '25 09:09

Rono


1 Answers

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.

like image 105
Felix Glas Avatar answered Sep 04 '25 23:09

Felix Glas