C++ C++;获得;调试错误R6010-已调用中止();当我给线程分配承诺时

C++ C++;获得;调试错误R6010-已调用中止();当我给线程分配承诺时,c++,multithreading,promise,future,C++,Multithreading,Promise,Future,我得到一个错误: 在我的代码中: 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 (

我得到一个错误:

在我的代码中:

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;}
bool列表文件(wstring路径、wstring掩码、向量和文件){
句柄hFind=无效的句柄值;
WIN32_查找_数据ffd;
wstring规范;
堆栈目录;
目录。推送(路径);
clear()文件;
而(!directories.empty()){
path=directories.top();
spec=路径+L“\\”+掩码;
目录pop();
hFind=FindFirstFile(规范c_str(),&ffd);
if(hFind==无效的句柄值){
返回false;
}
做{
如果(wcscmp(ffd.cFileName,L.“)!=0和&wcscmp(ffd.cFileName,L.“)!=0){
if(ffd.dwFileAttributes和文件属性目录){
目录.push(路径+L”/“+ffd.cFileName);
}
否则{
文件。推回(路径+L”/“+ffd.cFileName);
}
}
}while(FindNextFile(hFind,&ffd)!=0);
如果(GetLastError()!=错误\u没有\u更多\u文件){
FindClose(hFind);
返回false;
}
FindClose(hFind);
hFind=无效的句柄值;
}
返回true;}
void findText(std::string文件名、std::string单词、promise和prom){
std::iff流(文件名);
std::字符串s;
std::string notFound=“否”;
bool-found=false;
如果(!f){

std::cout让我们仔细看看这些行:

for (...) {
    ...
    std::promise<string> prom;
    ...
    std::thread(findText,str,search,std::ref(prom)).detach();
}
为此,您可能需要使线程函数将promise参数作为右值引用:

void findText(std::string filename, std::string word , promise<string> &&prom) {
    ...
}
然后你创建了一个像线一样的

auto prom = std::make_unique<std::promise<std::string>>();
// Or if you don't have std::make_unique
//   std::unique_ptr<std::promise<std::string>> prom(new std::promise<std::string>);
futures.push_back(prom->get_future();
std::thread(findText,str,search,std::move(prom)).detach();

让我们仔细看看以下几行:

for (...) {
    ...
    std::promise<string> prom;
    ...
    std::thread(findText,str,search,std::ref(prom)).detach();
}
为此,您可能需要使线程函数将promise参数作为右值引用:

void findText(std::string filename, std::string word , promise<string> &&prom) {
    ...
}
然后你创建了一个像线一样的

auto prom = std::make_unique<std::promise<std::string>>();
// Or if you don't have std::make_unique
//   std::unique_ptr<std::promise<std::string>> prom(new std::promise<std::string>);
futures.push_back(prom->get_future();
std::thread(findText,str,search,std::move(prom)).detach();

您是否尝试在调试器中运行以准确定位代码中发生的位置?您能否至少指出代码中显示发生位置的那一行,并附上注释(英文)例如,使用Debug i接受了2个错误:在STDGTHEADHEX.EXE中的0x75 45 C41F中未处理的异常:微软C++异常:STD::FuturyStor在内存位置0x00 3AF700。运行时检查失败0。ESP的值没有在函数调用中正确保存。这通常是调用一个调用Co声明的函数的结果。使用使用不同调用约定声明的函数指针的nvention。当我创建承诺并将其分配给线程时,问题一定在主函数中。如果在调试器中运行,程序将在您得到
中止
时停止。然后您将能够向上移动函数调用堆栈到代码,并准确地看到在你的代码中,它发生了。我刚刚在最后一个for循环中添加了一个try{}catch{}块,它抛出和异常:“违背承诺”你是否尝试在调试器中运行,以准确定位代码中它发生的位置?你能至少指出代码中显示它发生位置的那一行,并加上注释(英文)吗例如,使用Debug i接受了2个错误:在STDGTHEADHEX.EXE中的0x75 45 C41F中未处理的异常:微软C++异常:STD::FuturyStor在内存位置0x00 3AF700。运行时检查失败0。ESP的值没有在函数调用中正确保存。这通常是调用一个调用Co声明的函数的结果。使用使用不同调用约定声明的函数指针的nvention。当我创建承诺并将其分配给线程时,问题一定在主函数中。如果在调试器中运行,程序将在您得到
中止
时停止。然后您将能够向上移动函数调用堆栈到代码,并准确地看到在你的代码中,它发生了。我刚刚在最后一个for循环中添加了一个try{}catch{}块,它抛出和异常:“违背承诺”以及如何解决这个问题并跟踪承诺?最后一行不是你问题的答案吗?修改我得到的:…线程已退出代码3(0x3)。使用你的答案std::move(prom),我必须修改函数findText taking(promise prom)而不是(promise&prom),但随后出现以下错误:错误2错误C2280:'std::promise::promise(const std::promise&'):尝试引用已删除的函数c:\program files(x86)\microsoft visual studio 12.0\vc\include\functional 1149 1 std_Thread@A.Martino更改线程函数以将承诺作为右值引用(即
std::promise&&prom
)。如何解决此问题并跟踪承诺?最后一行不是您问题的答案吗?修改此行我获得:…线程已退出,代码为3(0x3)。使用您的答案std::move(prom),我必须修改函数findText taking(promise prom),而不是(promise&prom)但是我有一个错误2错误C2280:'std::promise::promise(const std::promise&'):尝试引用已删除的函数c:\program files(x86)\microsoft visual studio 12.0\vc\include\functional 1149 1 std_Thread@A.Martino更改线程函数以将承诺作为右值引用(即
std::promise和&prom
)。