Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How lock a file in windows using c++?

How can I lock a file in windows to only current thread (no other threads from same process and no other processes) can access (read/write) the file?

If it is possible please tell me some fcntl-like solution (solution which locks file having its descriptor). But in any case other solutions are welcome too.

like image 494
Mihran Hovsepyan Avatar asked Sep 02 '25 18:09

Mihran Hovsepyan


1 Answers

In Windows, you can open a file with exclusive access with the API function CreateFile and specifying 0 as the share mode. More details at this MSDN link, and this MSDN link.

like image 140
Chad Avatar answered Sep 04 '25 08:09

Chad