C语言 如何停止生成多个线程的进程



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

从技术上讲,在程序中的任何位置使用 exit()_exit()并不意味着正常终止,并且终止时的程序输出是不可预测的。此外,还可以查看exit()_exit()的比较。

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

下面是一个具有两个线程(T1T2 (的进程 ( P ( 的简单示例。像这样:

   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!