File Management


I am new to C++. I got a project. It is a multi threading application. In this case my part is develop a log file creating library. I got some experience with file writing using single application. But in this case it's not proper. Because in this case their are many threads. many threads need to write the information on to same file. Other programmers need to write into the same log file from anywhere of the application.Please give me a solution for this problem.

Thank you.
One solution would be a global shared read/write queue that in turn drives a single fstream instance? You certainly wouldn't want the fstream to be directly accessible. Even with locks and mutexes available I can see that getting complicated quickly. I have never really had to deal with multithreaded container access though so I'll leave that to others.
There are very many C++ logging libraries.

boost.log (a boost candidate)
Apache's log4cxx:
google's glog:

and I bet i can google up a lot more. Why write Yet Another one?

That said, just for your case, either make the log function acquire a mutex and not release until it's done writing to the filestream, or if you want logging to be non-blocking, set up a queue.
Topic archived. No new replies allowed.