我有以下代码:
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <thread>
#define BOOST_THREAD_DYN_LINK
#include "boost/filesystem.hpp"
#include "boost/thread/thread.hpp"
using namespace std;
void testEx(std::string & name){
while (1){
boost::filesystem::path perc(name);
if (boost::filesystem::exists(perc))
cout << "yes" << endl;
}
}
int main(){
std::string name = "c:\\text.txt";
vector<boost::thread> pool;
for (int i = 0; i < 10;i++)
pool.emplace_back(testEx, name);
while (1){
std::ofstream out(name, std::ios_base::out | std::ios_base::app);
out << "a" << std::endl;
out.close();
}
for (auto & t : pool)
t.join();
}
主线程是写一个文件。使用子线程我检查文件是否存在。程序经常在我创建boost :: filesystem :: path实例的行中因内存损坏而崩溃。
如果我使用std :: thread而不是boost :: thread,程序运行正常。
我能做些什么来让这个程序与boost :: thread(许多遗留代码)一起工作。为什么会这样?
答案 0 :(得分:0)
到现在为止我通过动态连接boost :: filesystem来解决
#define BOOST_FILESYSTEM_DYN_LINK
无论如何只是一种解决方法