Boost :: filesystem, std :: getenv and concurrency

Suppose I have the following code:

#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();
}

      

It fails at runtime with the following error:

* Internal program error - assertion (codecvt_facet_ptr ()) failed in class const std :: codecvt & __ cdecl elevation :: file system :: path :: codecvt (Invalid): libs \ filesystem \ src \ path.cpp (888) : codecvt_facet_ptr () the facet has not been properly initialized

I read in the documentation that it is not thread safe in terms of concurrent set and get operations, not multiple get operations as in my case. The _wgetenv function works the same.

Why? What am I doing wrong?

MSVC-12.0, boost 1.55, Windows 8

Thanks in advance.

+3


source to share


1 answer


It seems that some versions of VS have error messages like: here

It is supposed to be initialized before your threads:



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
}

      

+1


source







All Articles