C++ 使用Boost 1.55线程和文件系统的并发内存损坏(Visual Studio 2013)
我有以下代码:C++ 使用Boost 1.55线程和文件系统的并发内存损坏(Visual Studio 2013),c++,multithreading,boost,concurrency,filesystems,C++,Multithreading,Boost,Concurrency,Filesystems,我有以下代码: #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;
#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\u THREAD\u DYN\u链接
#包括“boost/filesystem.hpp”
#包括“boost/thread/thread.hpp”
使用名称空间std;
void testEx(标准::字符串和名称){
而(1){
boost::filesystem::path perc(名称);
如果(boost::filesystem::exists(perc))
现在我也通过动态链接boost::filesystem解决了这个问题
#define BOOST_FILESYSTEM_DYN_LINK
不管怎样,这只是一个解决办法可能是因为您使用未同步的iostreams(例如,std::cout
)而实际失败了吗?请尝试在所有输出流周围使用互斥锁,或者尝试在没有任何实际控制台输出的情况下运行。