0

Is there, on Windows, any easy way to lock a file in an exclusive way (reading and writing for my software) in multithread C code? I've tried the LockFileEx but it works only between process and not for thread(of the same process).

Note: My problem is that i've made a little file server(multithread), when a request for a file comes, one thred "A" must acces in exclusive way to the requested "file1.txt", but if another thread "B" wants the same "file1.txt" it has to wait before to use this file. If Thread "A" uses the CreateFile() with dwSharedMode to "0" for open/create "file1.txt", ensures that only it read or open this file, infact error happens if thread "B" tries to open the "file1.txt". Now how thread "B" can wait on "file1.txt"?

Community
  • 1
  • 1
  • I would argue that you should know what your threads are doing, and implement locking yourself (with a [Mutex](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682411(v=vs.85).aspx), for example). – Jonathon Reinhart Mar 25 '13 at 02:55
  • Look below at Aniket answer. File locking is heftier than mutex or thread locking. It also works across clustered servers or shared drives. For instance the same directories and files is being used by your mini-server running on two different computers. – TamusJRoyce Apr 23 '14 at 18:32

3 Answers3

1

You can open the file in exclusive mode by setting dwShareMode to 0 in CreateFile() function.

Read more here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363874(v=vs.85).aspx

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Note: My problem is that i've made a little file server(multithread), when a request for a file comes, one thred "A" must acces in exclusive way to the requested "file1.txt", but if another thread "B" wants the same "file1.txt" it has to wait before to use this file. If Thread "A" uses the CreateFile() with dwSharedMode to "0" for open/create "file1.txt", ensures that only it read or open this file, infact error happens if thread "B" tries to open the "file1.txt". Now how thread "B" can wait on "file1.txt"? – user2199646 Mar 25 '13 at 15:21
  • Files are inherently thread-safe. Just try to open the file. If there is an exclusive lock on the file, then wait. That's your locker right there. It is guaranteed atomic, across threads or across processes. – TamusJRoyce Apr 23 '14 at 18:28
1

I understand you question as the following one : how to organize access to the resource(file) for mutliply threads(readers - writers problem should be solved).

If your resource should be shared amoung threads that operate within the same process:
use synchronization primitive critical_section (it is more efficient than system objects synchronization primitives, but works only for threads within the same process)
otherwise use mutex;

spin_eight
  • 3,925
  • 10
  • 39
  • 61
0

Slim Reader/Writer (SRW) Locks

SRW locks provide two modes in which threads can access a shared resource:

  • Shared mode, which grants shared read-only access to multiple reader threads, which enables them to read data from the shared resource concurrently. If read operations exceed write operations, this concurrency increases performance and throughput compared to critical sections.

  • Exclusive mode, which grants read/write access to one writer thread at a time. When the lock has been acquired in exclusive mode, no other thread can access the shared resource until the writer releases the lock.

Scy
  • 488
  • 3
  • 11