Ich habe den folgenden Code:
#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();
}
Der Hauptthread ist das Schreiben einer Datei. Mit Kind-Threads überprüfe ich, ob die Datei existiert. Das Programm stürzt oft mit Speicherbeschädigung in der Zeile ab, in der ich eine boost :: filesystem :: path-Instanz erstelle.
Wenn ich std :: thread anstelle von boost :: thread verwende, funktioniert das Programm gut.
Gibt es etwas, was ich tun kann, um dieses Programm mit boost :: thread (viel Legacy-Code) zu arbeiten. Warum passiert dies?
Antworten:
0 für die Antwort № 1Mittlerweile habe ich das Problem gelöst, indem ich auch das boost :: filesystem dynamisch verknüpfe
#define BOOST_FILESYSTEM_DYN_LINK
nur ein Workaround sowieso