[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?

AMDG James Madison wrote:
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?
It is not safe. Look at the declaration of operator% template<class T> basic_format& operator%(const T& x) This operation modifies the format object and is therefore intrinsically not thread-safe. In Christ, Steven Watanabe

James Madison wrote:
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? I'm pretty sure the thread safety of Format is the standard mantra of "one instance from one thread". That is, you can access any given instance only from one thread unless you synchronize, but multiple instance in multiple threads do not interfere with each other.
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