boost :: filesystem,std :: getenv和并发

Fro*_*art 5 c++ boost boost-filesystem

假设我有以下代码:

#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>

void foo()
{
  const boost::filesystem::wpath& appdata_folder = std::getenv("APPDATA");
  while (true)
  {
    boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
  }
}

int main()
{
  boost::thread first(foo);
  boost::thread second(foo);

  first.join();
  second.join();
}
Run Code Online (Sandbox Code Playgroud)

它在运行时失败,并显示以下错误:

* Internal Program Error - assertion (codecvt_facet_ptr()) failed in const class std::codecvt &__cdecl boost::filesystem::path::codecvt(void): libs\filesystem\src\path.cpp(888): codecvt_facet_ptr() facet hasn't been properly initialized

In the documentation I read that it's not thread-safe in terms of parallel set and get operations, not the several get operations as in my case. _wgetenv function works the same way.

Why? What am I doing wrong?

MSVC-12.0, boost 1.55, Windows 8

Thanks in advance.

doc*_*ove 3

对于某些版本的 VS 和 boost,似乎有关于此的错误报告:例如这里

建议在线程之前初始化:

void make_loc(std::locale& loc)
{
    loc = default_locale();
}

int main()
{
    std::locale loc;

    boost::once_flag once;
    boost::call_once(once, boost::bind(&make_loc, boost::ref(loc)));
    //as you were
}
Run Code Online (Sandbox Code Playgroud)