dimanche 12 février 2023

Why the below program aborts when a std::vector<:future>> is used.?

I wanted to perform hashing of a stream of input messages in multithreading, so was trying to implement

std::vector<std::future<HashData>> futures;

but the program aborts from abort.h when debugging in Visual Studio 2019.

Code Snippet:

std::vector<std::future<HashData>> futures;
std::vector<std::string> messages;

for (int i = 0; i < messages.size(); i++)
{
  std::promise<HashData> promiseHashData;
  std::future<HashData> futureHashData = promiseHashData.get_future();
  futures.emplace_back(std::move(futureHashData));
  std::async(std::launch::async, [&]() {PerformHash(std::move(promiseHashData), messages[i]);});
}

std::vector<HashData> vectorOfHashData;
// wait for  all async tasks to complete
for (auto& futureObj : futures)
{
  vectorOfHashData.push_back(futureObj.get());
}

void PerformHash(std::promise<HashData>&& promObject, std::string& message)
{
    ComputeHashUsingSHA256(message);
        HashData data;

    // set data for HashData object
    data.i = i;
    data.blocks = blocks;
    data.blocksize = blocksize;
    data.blockbufs = blockbufs;
    data.secs = secs;
    memcpy(data.digest, digest, SHA256_DIGEST_SIZE);

    data.has_hashdata = has_hashdata;
    memcpy(data.hashdata_buf, hashdata_buf, c_hashsize);

    promObject.set_value(data);

}

while debugging the code, observed as only few threads were created using async and post that, the program aborts from abort.h as shown in this image

Aucun commentaire:

Enregistrer un commentaire