 So in one of the logging articles, boost::thread_specific_ptr was mentioned. In my opinion, it is a very cool little thing. It's basically just a pointer which can have a different value in each thread. Why would you want one? Well, I used to have a bit of a problem. Angel script, the scripting language I use, requires a thing called a context when calling script functions. Contexts cannot be shared between threads and I wanted to cache the context between calls to the scripting language, since creating them is relatively expensive. Calls are made by a couple of different threads, so caching the context required a bit of hackery. A thread specific pointer, however, is a perfect fit. For each call to script I check if the context for the thread is null (unlike a normal pointer it is initialised to null by default) and if it is I create the context. Otherwise I just use the one already cached for this thread.
Also, this is what my log files look like now:
Log file
They're quite colourful. Completely overkill, too. But the really great thing is that I'm now happy to look at log files. I want the program to go up in flames just so I have an excuse to read their christmasy goodness! Though I suppose the novelty will wear off.
|