File segment/section/record locks in Linux threads

前端 未结 4 1715
萌比男神i
萌比男神i 2021-01-28 16:42

I have a multi-threaded process where a file is shared (read and written) by multiple threads. Is there any way a thread can lock one file segment so that other threads cannot a

4条回答
  •  迷失自我
    2021-01-28 17:15

    There are 2 ways you can do it:

    1. Use Mutex to get a record's lock in a thread within the same process. Once the lock is acquired, any other thread in the process, mapping the file that tries to acquire the lock is blocked until the lock is released.(Preferable and only most straightforward solution available in Linux).

    2. Semaphores and mutexes on a shared memory or a memory mapped file.

提交回复
热议问题