[format] Thread safety of boost::format

I was browsing the boost::format docs and did not find anything about the Thread safety of boost::format. Essentially I want to craft a bunch of error handling methods like this: void ProcessError(...args...) { static boost::format formatter(...); //Try to save the cost of creating this for every single error //do stuff LOG(boost::str(formatter % ...args...); } This method would be called from many, many threads. Is this considered safe? Is there some general information about the thread safety of boost::format methods somewhere in the docs?

James Madison wrote:
In other words, what you're doing is most definitely not thread-safe. Think about it this way: every time you use operator %, Format formats the argument and inserts it into its list of formatted arguments. Unless this list is thread-local, that cannot possibly be thread-safe, and it cannot be thread-local for both performance- and correctness reasons. You can 1) Not make the formatter static. 2) Make a static formatter but simply copy it to a local instance before modifying it; that might save the cost of parsing. 3) Make the formatter static and completely serialize ProcessError. This would probably negate any performance gain. You shouldn't do #2 unless you can actually prove that format construction takes significant time, of course. Sebastian
participants (3)
-
James Madison
-
Sebastian Redl
-
Steven Watanabe