使用Boost 1.55线程和文件系统并发内存损坏(Visual Studio 2013)
Concurrency memory corruption using Boost 1.55 Thread and Filesystem (Visual Studio 2013)
我有以下代码:
#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::线程(很多遗留代码)。为什么会发生这种情况?
现在我通过动态链接boost::filesystem来解决
#define BOOST_FILESYSTEM_DYN_LINK
只是一个变通
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 性能报告 (.vspx) 在 Visual Studio 2013 中未打开(显示损坏)
- 在Visual Studio中生成的匿名命名空间变量的损坏名称的数字部分是什么意思
- 为什么我的visual c++在堆栈损坏的情况下没有触发堆栈损坏错误
- Visual C 构建工具2015-损坏的安装
- Visual Studio在使用sockaddr_storage时会损坏内存
- TCP/IP IOCP接收到的数据有时已损坏-Windows上的Visual C++
- Visual Studio 2008:字符串文字"??-"、"??'"、"??="损坏
- 在释放动态内存时,Visual Studio 中的堆损坏错误
- 在 Visual C++ 中删除调用后检测到堆损坏
- Visual Studio的生成已损坏
- 为什么Windows Visual Studio会损坏C样式字符串,而Linux不会?
- 在Visual Studio调试模式下运行时,c++对象引用已损坏
- 变量损坏c++字符串Visual Studio 2005
- 使用Boost 1.55线程和文件系统并发内存损坏(Visual Studio 2013)
- Visual studio c++库文件损坏/损坏.的帮助
- Visual Studio - 通过 VS 6.0 执行 CL.EXE C++会导致"驱动器中没有磁盘"。<损坏的驱动器名称>