Global Locking ?
-
I got a program running in multiple processes on the same machine and all of them are writing to the same log file. I want to make sure that they are not trying to write to the log at the same time. And need some sort of locking or criticalsection type of thing that works over different processes. Any one got any tips?
-
I got a program running in multiple processes on the same machine and all of them are writing to the same log file. I want to make sure that they are not trying to write to the log at the same time. And need some sort of locking or criticalsection type of thing that works over different processes. Any one got any tips?
Use a mutex, it works like a critical section but is visible to all processes on the machine. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- You cannot stop me with paramecium alone!
-
I got a program running in multiple processes on the same machine and all of them are writing to the same log file. I want to make sure that they are not trying to write to the log at the same time. And need some sort of locking or criticalsection type of thing that works over different processes. Any one got any tips?
If you are also guarding against multiple USER sessions, you need to be aware of terminal services issues, and also to apply more than 'NONE', EMPTY', or 'NULL DACL' security on the synchronization object or no one else will be able to open it up right all the time. I am pulling my few remaining hairs out sorting this all out for terminal services and Windows XP Fast User Switching.
-
Use a mutex, it works like a critical section but is visible to all processes on the machine. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- You cannot stop me with paramecium alone!
You probably want it to be a named mutex unless your log code is in a common dll and you have declared the mutex in a shared section. John
-
I got a program running in multiple processes on the same machine and all of them are writing to the same log file. I want to make sure that they are not trying to write to the log at the same time. And need some sort of locking or criticalsection type of thing that works over different processes. Any one got any tips?
Aside from the mutex, you can open your log file exclusively. I believe the CFile::Open function returns a bool if the file opens, so if your file doesn't open, it means somebody else (another app) is using that file, so just let your app keep trying until it gets access. I wouldn't go trying this on a file where more than a few applications (threads) are going to be using it, but it's a cheap little way of assuring you have exclusive access to your file. However, the code can get a little tricky because you also need to make sure that the file actually exists before you enter a loop that tries to open it exclusively...otherwise your program will go into an infinite loop. The only real benefit of using this method is that you can share your log file on a network, and have your application on multiple computers share it...don't know if you're getting that complex or not (if you are, look into using a database instead.) Good luck