gpt4 book ai didi

C++ 终止线程

转载 作者:行者123 更新时间:2023-11-30 05:39:37 24 4
gpt4 key购买 nike

我知道这个问题已被多次询问和回答,而且我知道结束线程的最佳方法是使用标志并在请求时退出线程函数。这也是我目前正在做的,但我的问题是线程函数运行了很长时间,所以有时我需要等待几个小时直到线程结束。我的线程函数看起来像这样:

void threadfunction()
{
while(!exitRequested)
{
doSomeWork();
object1.doSomeWork();
object2.doSomeWork();
while(somecondition)
{
object3.doSomeWork();
}
object4.doSomeWork();
}
}

这只是一个示例,实际上代码看起来要复杂得多。但我想证明的是,我调用了几个类方法,这些方法可能需要几个小时才能完成(每个函数)。

所以我现在正在做的是检查函数之间是否请求退出(例如 object1.doSomeWork();object2.doSomeWork(); 之间)但是正如我所说的函数调用可能需要几个小时,所以我需要检查是否在这些功能中请求退出。为此,我需要通过 exitRequested标记这些函数,但在我看来这看起来不太好,可能会有更好的解决方案。

我能想到的一个解决方案是通过抛出异常来创建如下内容:

void threadfunction()
{
try {
while(!exitRequested)
{
...
}
} catch (const ExitRequestException &e) {}
}

但随后需要以某种方式引发异常。据我所知,我不能在一个线程中从另一个线程引发异常,对吧?

您有更好的解决方案吗?或者你认为我真的需要通过 exitRequested标记所有这些函数并污染我的代码?

最佳答案

谷歌搜索了一段时间后,我发现(并创建了)一个对我来说非常合适的答案,尽管它只适用于 pthreads。对于非 pthreads,我可以想象使用描述的概念 here .

那么我现在在做什么:我正在向工作线程发送信号。此线程在信号处理程序中处理自定义信号并引发异常。此异常在线程函数的外部出现。通过使用这个概念,我的优势在于抛出异常并展开堆栈,因此我可以关闭所有打开的资源。这是我完整的工作示例:

#include <thread>
#include <signal.h>
#include <unistd.h>
#include <iostream>

using namespace std;

//Custom exception which is used to stop the thread
class StopException {};

void sig_fun(int s)
{
if(s == SIGUSR2)throw StopException();
}

void threadFunction()
{
cout<<"Thread started"<<endl;
try {
while(true)
{
//Work forever...
sleep(1);
}
} catch(const StopException &e) {
cout<<"Thread interrupted"<<endl;
}
cout<<"Thread stopped"<<endl;
}

int main(int argc, char *args[])
{
//Install Signal handler function
signal(SIGUSR2, sig_fun);
pthread_t threadObject;
thread t([&threadObject]()
{
//Store pthread_t object to be able to use it later
threadObject = pthread_self();
threadFunction();
});

string temp;
cout<<"Write something when you want the thread to be killed"<<endl;
cin>>temp;

//Send Signal to thread
pthread_kill(threadObject, SIGUSR2);
t.join();
}

关于C++ 终止线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com