【问题标题】:How to stop a process that has spawned multiple threads如何停止产生多个线程的进程
【发布时间】:2018-06-23 03:19:35
【问题描述】:

我有一个产生多个线程的进程。现在,要停止并退出进程,我可以使用_exit(0)。但这是否处理所有线程的正确终止?有什么方法可以杀死一个进程(在其内部)并正确终止其所有线程。

【问题讨论】:

  • 这与特定语言有关吗?请添加正确的语言标签。
  • 代码在 C 中
  • 对。请编辑您的问题并添加C 标签。通过我的回答。我已经用详细的 cmets 详细说明了代码。我希望这能帮助您了解您的问题并解决它。

标签: c multithreading


【解决方案1】:

从技术上讲,在程序中的任何位置使用exit() 或_exit() 并不意味着正常终止,并且终止时的程序输出将无法预测。另外,也可以看看exit() vs _exit()的对比。

由于没有语言标签,我将使用伪代码(可能是 C++11)详细说明这个问题。从技术上讲,这是一个与语言无关的问题。对于不同的编程语言,线程 API 或多或少是相同的。

下面是一个简单的进程示例 (P),它有两个线程(T1 和 T2)。像这样的:

   P
  / \
 /   \
T1   T2

现在,我们希望这些线程在 1 秒后在屏幕上重复打印一些问候消息。这是完整的示例:

// Headers ...

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;

// You need something to signal your thread that it should exit.
// So, we're using a global variable to store this information.

volatile bool is_exited = false;

// Here is our callback() or long running task that
// our threads would be executing

void callback()
{
    // This loop should exit once is_exited flag's condition is true.
    // This loop body is actually indicating the actual job that the
    // thread needs to execute.

    auto id = this_thread::get_id();

    cout << id << ". Started!\n";

    while( !is_exited )
    {
        cout << id << ". Hello!";
        this_thread::sleep_for( 1s );
    }

    cout << id << ". Exited!\n";
}

// Here is our main() function that would start two threads,
// register the callback, and run it. After doing its own processing,
// the main() function would signal threads to stop and wait for them
// to finish their running task and get out of the callbacks. Usually,
// there's a join() or wait() function call to do this. C++ has join().

int main()
{
    cout << "main() started!\n";

    // Now, register your callback with your threads
    // C++ threads start right away after this
    // You don't have to call some start() function

    thread t1 { callback };
    thread t2 { callback };

    // So, now that the threads have started the main()
    // can do its own things. You could use simple sleep here
    // to give some artificial wait.

    for ( int i = 0; i < 1000000; ++i )
    {
        // ...
    }

    // Now, we need to send the signal to threads to stop their
    // execution for a graceful termination.

    is_exited = true;

    // There's a call to join() function to wait for the
    // threads to exit; so here the main() function is waiting
    // for the threads to exit. Once the callback() finishes,
    // the thread would exit and the main() function would get out
    // of these join() calls. And, the program would exit gracefully.

    t1.join();
    t2.join();

    cout << "main() exited!\n";

    return 0;
}

以下是实际示例:https://ideone.com/NEuCe9
我已经调整了睡眠时间以获得良好的输出。观察输出。专门查找线程 ID。

以下是此现场示例的输出:

main() started!
47277466478336. Started!
47277466478336. Hello!
47277464377088. Started!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Exited!
47277464377088. Exited!
main() exited!

【讨论】:

    猜你喜欢
    • 2014-05-29
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多