【发布时间】:2016-05-18 09:17:40
【问题描述】:
我收到错误消息:Debug Error R6010 -abort() has been called
在我的代码中:
bool ListFiles(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"/" + ffd.cFileName);
}
else {
files.push_back(path + L"/" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;}
void findText(std::string filename, std::string word , promise<string> &prom) {
std::ifstream f(filename);
std::string s;
std::string notFound = "no";
bool found = false;
if (!f) {
std::cout << "il file non esiste"<<std::endl;
}
while (f.good()) {
std::getline(f, s);
if (s.find(word, 0) != string::npos) {
found = true;
}
}
if (found) {
//cout << "Parola trovata in -> " << filename << endl;
prom.set_value_at_thread_exit(filename);
}
else {
prom.set_value_at_thread_exit(notFound);
}
f.close();}
int main(int argc, char* argv[]){
//vector<std::thread> th;
vector<future<string>> futures;
vector<wstring> files;
string search = "ciao";
string notFound = "no";
if (ListFiles(L"pds", L"*", files)) {
for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it) {
//wcout << it->c_str() << endl;
wstring ws(it->c_str());
string str(ws.begin(), ws.end());
// Show String
//cout << str << endl;
//Creo una promise per ogni thread in cui andrò a cercare il risulato
std::promise<string> prom;
futures.push_back(prom.get_future());
std::thread(findText,str,search,std::ref(prom)).detach();
}
}
for (int i = 0; i < futures.size(); i++){
futures.at(i).wait();
if (futures.at(i).get().compare(notFound)!=0)
cout << "Parola trovata in ->" <<futures.at(i).get()<<endl;
}
return 0;}
我之前尝试过不使用 Promise 并让每个线程在找到 word 并且它工作时打印文件名。 所以我不知道为什么使用承诺和未来来检索这个值会导致我这个问题...... 我正在使用 VS 2013
【问题讨论】:
-
您是否尝试在调试器中运行以准确定位代码中发生的位置?你能至少指出代码中你显示它发生的地方吗,例如用注释(英文)?
-
使用调试我收到这 2 个错误:std_Thread.exe 中 0x7545C41F 处的未处理异常:Microsoft C++ 异常:内存位置 0x003AF700 处的 std::future_error。 和 运行-时间检查失败#0 - ESP 的值未在函数调用中正确保存。这通常是调用使用一种调用约定声明的函数和使用不同调用约定声明的函数指针的结果。 当我创建承诺并将其分配给线程时,问题必须出在主函数中。
-
如果你在调试器中运行,程序会在你得到
abort时停止。然后,您将能够将函数调用堆栈上移至您的代码,并确切地查看它在代码中发生的位置。 -
我刚刚在最后一个 for 循环中添加了一个 try{} catch{} 块,它抛出异常:“broken promise”
标签: c++ multithreading promise future