如何创建新线程并在一段时间后终止它? [英] How to make a new thread and terminate it after some time has elapsed?

查看:93
本文介绍了如何创建新线程并在一段时间后终止它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交易是:

我想创建一个与在Windows中执行新.exe类似的线程,因此,如果该程序(新线程)崩溃或进入无限循环:它将被优雅地杀死(超过时间限制后,或者当它终止时)崩溃)并正确释放所有资源.

I want to create a thread that works similarly to executing a new .exe in Windows, so if that program (new thread) crashes or goes into infinite loop: it will be killed gracefully (after the time limit exceeded or when it crashed) and all resources freed properly.

当该线程成功执行后,我希望能够修改一些可能包含一些数据的全局变量,例如文件列表.这就是为什么我不能从Windows执行外部可执行文件的原因,因为我无法访问已执行到新线程中的函数内部的变量.

And when that thread has succeeded, i would like to be able to modify some global variable which could have some data in it, such as a list of files for example. That is why i cant just execute external executable from Windows, since i cant access the variables inside the function that got executed into the new thread.

编辑:进一步澄清了该问题.

Clarified the problem a lot more.

推荐答案

线程不是某种可以用来做事的神奇对象.这是通过代码执行的单独路径.除非您专门对其进行编程,否则无法使您的代码在其代码库中随意跳转.即便如此,它也只能在C ++的规则内完成(即:调用函数).

A thread is not some sort of magical object that can be made to do things. It is a separate path of execution through your code. Your code cannot be made to jump arbitrarily around its codebase unless you specifically program it to do so. And even then, it can only be done within the rules of C++ (ie: calling functions).

您不能杀死线程,因为杀死线程将彻底破坏程序员所做的一些最基本的假设.现在,您必须考虑下一行由于您既无法预测也无法阻止的原因而无法执行的可能性.

You cannot kill a thread because killing a thread would utterly wreck some of the most fundamental assumptions a programmer makes. You would now have to take into account the possibility that the next line doesn't execute for reasons that you can neither predict nor prevent.

这与异常处理不同,在异常处理中,C ++特别要求调用析构函数,并且您可以捕获异常并进行特​​殊清理.您正在谈论执行一段代码,然后突然结束整个调用堆栈的执行.那是行不通的.

This isn't like exception handling, where C++ specifically requires destructors to be called, and you have the ability to catch exceptions and do special cleanup. You're talking about executing one piece of code, then suddenly ending the execution of that entire call-stack. That's not going to work.

Web浏览器从每个选项卡的线程"模式转移到每个进程的选项卡"模型的原因完全是这样的:因为进程可以终止,而无需将其他进程留在未知状态.您需要使用进程而不是线程.

The reason that web browsers moved from a "thread-per-tab" to "process-per-tab" model is exactly this: because processes can be terminated without leaving the other processes in an unknown state. What you need is to use processes instead of threads.

当进程完成并设置数据时,您需要使用一些进程间通信系统来读取该数据(我自己喜欢Boost.Interprocess).它看起来不像常规的C ++全局变量,但是您阅读它应该没有问题.这样,如果花费的时间太长,您可以有效地终止该进程,并且您的程序将保持在合理的状态.

When the process finishes and sets it's data, you need to use some inter-process communication system to read that data (I like Boost.Interprocess myself). It won't look like a regular C++ global variable, but you shouldn't have a problem with reading it. This way, you can effectively kill the process if it's taking too long, and your program will remain in a reasonable state.

这篇关于如何创建新线程并在一段时间后终止它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆