0

下面是我用来尝试运行 packaged_task 的代码。但是,当我尝试编译时,我收到错误:

'std::function<_Ret(CStore &, FileDetails &)>::function(std::function<_Ret(CStore &, FileDetails &)> &&)': cannot convert argument 1 from 'long(__cdecl &)(CStore &, std::vector<FileDetails, std::allocator<_Ty>> &)' to 'std::nullptr_t'

在将第二个参数添加到 CheckIfFilesExist 之前,我已经完成了这项工作,但是一旦我添加了第二个参数,事情就开始变得不稳定。非常感谢任何见解!

HRESULT CheckIfFilesExist(CStore& store, std::vector<FileDetails>& files)
{
    std::tcout << _T("Starting store ") << store.m_number << std::endl;
    HRESULT hr = store.CheckIfFilesExist(files);
    std::tcout << _T("Finished store ") << store.m_number << std::endl;
    return hr;
}

HRESULT RunThreads(std::vector<CStore>& stores, std::vector<FileDetails>& fileDetails)
{
    std::vector<std::future<HRESULT>> vecFutures;
    std::vector<std::thread> vecThreads;
    size_t completed = 0;
    size_t totalRan = 0;
    int running = 0;

    while(completed < stores.size()){
        if(running < 5 && totalRan < stores.size()){
            if(totalRan == stores.size() && completed == stores.size()){
                break;
            }

            ++running;
            std::packaged_task<HRESULT(CStore&, FileDetails&)> task(CheckIfFilesExist);
            vecFutures.push_back(std::move(task.get_future()));
            std::thread(std::move(task), std::ref(stores[totalRan]), fileDetails);
            vecThreads.push_back(std::thread(std::move(task), std::ref(stores[totalRan]), std::ref(fileDetails)));
            ++totalRan;
        }

        for(auto& future : vecFutures){
            if(future.valid() && future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready){
                future.get();
                ++completed;
                --running;
            }
        }
    }

    vecFutures.clear();

    for(std::thread& thread : vecThreads){
        thread.join();
    }

    return S_OK;
}

而 CStore 只是一个包含一些成员变量和这个函数的基类:

    HRESULT CStore::CheckIfFilesExist(const std::vector<FileDetails>& files)
{
    for(auto& file: files) {
        bool found = ::PathFileExists(m_path.c_str());
        if (TRUE == found) {
            m_filesFound++;
        } else {
            m_filesMissing++;
        }
    }
}
4

0 回答 0