dimanche 27 août 2017

std::async is blocking the process exit if the thread is created from a dll?

Scenario:

I have a console application (consoleApp) in which i use an exported function (exported_func) from another dll. In that function, i have started a new infinite thread. Here is the code -

EXPERIMENTAL_API int exported_func(void)
{
    auto f = []() {
        while (true)
        {
            cout << "H";
            this_thread::sleep_for(100ms);
        }
        return 1;
    };

    async_res.value = async(launch::async, f);
    return 1;
}

Now, from the main() function i have called this function and after that i started an infinite loop in the main function. Here is the code -

int main()
{
    exported_func();

    while (true)
    {
        cout << "L";
        this_thread::sleep_for(100ms);
    }

    return 0;
}

Problem:

The problem is that, if i want to close my console application by clicking the close button of the console, it takes 10 seconds to close the application.

And more, if i create a process (for the consoleApp.exe) using CreateChildProcess, then that process cant be terminated using ExitProcess.

But if i create the thread (async) from the main function(from consoleApp) then it closes immediately and can be terminated using ExitProcess.

Can anyone please explain whats happening here?

Aucun commentaire:

Enregistrer un commentaire