假设我有以下代码:
#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();
}
它在运行时失败并出现以下错误:
* 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
在documentation我读到它在并行设置和获取操作方面不是线程安全的,而不是像我的情况那样的几个获取操作。 _wgetenv 函数的工作方式相同。
为什么?我做错了什么?
MSVC-12.0, boost 1.55,Windows 8
提前致谢。
最佳答案
对于某些版本的 VS 和 boost,似乎有关于此的错误报告:例如here
建议在你的线程之前初始化:
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
}
关于c++ - boost::filesystem、std::getenv 和并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25619638/