How to terminate or stop a detached thread in c++?

前端 未结 4 772
旧巷少年郎
旧巷少年郎 2021-02-07 02:56

I am interested in terminating/stopping/killing a detached thread in c++. How can this be done?

void myThread()
{
    int loop = 0;
    while(true)
    {
                


        
4条回答
  •  长发绾君心
    2021-02-07 03:51

    In noticing that the straight forward answer of "NO" is less than helpful:

    Generally you will see the answer being akin to "use your own inter thread communication"

    Whether that is an atomic bool being shared between the main thread and the child thread or whether that is signalling a thread with another thread via OS methods.

    It may be helpful to shift the focus to the specifics of WHEN you would like a detached thread to die, rather than 'who does the killing'.

    https://en.cppreference.com/w/cpp/thread/notify_all_at_thread_exit

    Looking at code examples like the above helped me with what I was dealing with (main thread dying obscurely and children segfaulting) and I hope it helps you.

提交回复
热议问题