I'm new to Boost and I am redesigning a logging class that must work in a threaded application. It was designed to work like an ostream object but "did not work" (Said in Eddy Izz开发者_StackOverflow社区ard voice for those of you who know of his reference to Davinci's helicopter).
You might use it as follows:
Logger &logger = Logger::instance();
logger << "He designed a helicopter you know...that did not work." << endl;
So it was originally designed as a singleton and after a few approaches I thought perhaps Logger::instance() could return a Logger unique to the thread. I might use the STL to define a map between a Boost Thread ID and an instance of the Logger class.
Then I started thinking that perhaps there might be a smarter way to do this using some form of thread-local storage?
One issue with the map is that I must do some house cleaning to periodically delete Loggers when threads have terminated. I began to feel like I was reinventing things.
I see that Boost supports thread-local storage.
Any thoughts or caveats?
Thanks,
Kenny
I used the Boost thread-local storage and registered each logger with an object that funnels the data to either files or cout.
Kenny
精彩评论