Multithreaded streaming recording

I have a daemon that accepts socket connections and reads or writes a dynamic set of files depending on the nature of the connection. Since my daemon is multithreaded, it is possible that the same file can be written to more than one stream. Since my list of files is dynamic and not fixed, I'm not sure how to save the stream to another. For performance reasons, I want streams to be written to different files at the same time, and not the same file at the same time.

Other questions suggested using mutexes, but I don’t quite understand how the mutex will be used in this scenario - a list of files that are dynamic and only known to the stream.

Would it be wise to use file locking in this case? If so, how to implement file locking in a thread-safe manner?

+3
source share
3 answers

flockwill work fine. It does not block file descriptors; it blocks the actual file.

A file that was exclusively flock'ed cannot be completely blocked by another process or thread. This will break the whole purpose of the locks.

It should be noted that these locks are advisory. A process that does not use flockcan happily overwrite a file, even if another process has an exclusive flock'ed it.

+2
source

. socketing ( ()), , .

, , ... .

, , .

+1

, "" , - :

, :

  • , .

A. , mutex , . , . . . , .

Stream B. If the request comes for the same file, it will block the list and look for the file name contained in the list. If it is on the list, take the wait variable and wait. When the stream is signaled, take a lock on the list and see if the file is on the list (maybe another thread took a lock on the file name in front of you). If not, follow stream A. If so, take the wait variable in the new structure and wait until a message appears, and then repeat the steps above.

+1
source

Source: https://habr.com/ru/post/1792121/


All Articles