2

コンカレンシー ランタイムは、タスクによってスローされた例外を処理できず、"すばやく失敗する" ことを検出します。つまり、プロセスを終了します。に与えられた複数のサブタスクwhen_allが例外をスローする場合があります。理想的には、これらの例外がタスク ツリーをキャンセルし、最上位の get または wait で発生する単一の例外に集約されることを望みます。代わりに、私のプロセスは私の下で終了します。問題を示すおもちゃの例を次に示します。

#include <tchar.h>
#include <ppltasks.h>
#include <iostream>
#include <vector>

concurrency::task<int> CreateTask(int i)
{
    return concurrency::create_task([i]()
    {
        throw std::runtime_error("boo");
        return i;
    });
}

void Go()
{
    std::vector<concurrency::task<int>> tasks;

    tasks.push_back(CreateTask(1));
    tasks.push_back(CreateTask(2));

    auto allDone = concurrency::when_all(tasks.begin(), tasks.end());
    try
    {
        allDone.get();
    }
    catch (std::exception& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "Unexpected exception." << std::endl;
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    Go();
    std::cout << "Process is still running." << std::endl;
    return 0;
}

この例では、「Process is still running」がコンソールに出力される前にプロセスが終了します。サブタスクの例外によりwait_all、他のサブタスクが終了/キャンセルされるのを待たずにすぐに継続が呼び出され、他のサブタスクによって発生した例外を処理できず、プロセスが終了するようです。これは非常に望ましくないようです。回避策はありますか? これはバグですか?何か不足していますか?

4

0 に答える 0