c++boostthread-safetyatoflexical-cast

Is boost::lexical_cast thread-safe?


I am actually failing to find an answer to this question in boost documentation. I am being a bit paranoid about using atof in a multi-threaded environment, so one suggestion was to replace the call with lexical_cast. Is lexical_cast thread-safe?


Solution

  • Concurrent calls to lexical_cast are safe. But pay attention that it depends on (at least for my installed version of Boost) the currently installed C++ locale. Access to the installed C++ (as well as C) locale should be synchronized manually by the user. See the Data races section here. So, for example, concurrent calls to lexical_cast and std::locale::global are unsafe. Despite this, it is considered bad practice to change the installed C/C++ locale other than at program startup.