I have implemented multithreaded shared library in C++ (For Linux and Windows). I would like to add logging mechanism inside the library itself. The caller of the library is not aware of that. The log file would be same so I am wondering how could I design the thread safe logging if multiple process is using my library and trying to open and log into the same log file. Any suggestions?
I have implemented multithreaded shared library in C++ (For Linux and Windows). I would
Share
Use file locking. I believe fcntl is POSIX compliant so should work on Windows too. Does your code use Posix calls?
With fcntl, you should be able to lock a specific range of bytes. So if you seek to end and try to lock out the amount of bytes you are about to write, it should be pretty fast. To obtain the lock, you can probably spin, relinquishing the CPU for a small amount of time, if you don’t obtain the lock.