如何在不终止程序的情况下停止运行std :: thread [英] How to stop an std::thread from running, without terminating the program

查看:1465
本文介绍了如何在不终止程序的情况下停止运行std :: thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 C ++ 11 学习 std :: threads 来构建线程系统。 / p>

我想知道是否有一种方法可以在不终止整个程序的情况下停止线程的运行(不睡觉,但实际上是在破坏线程等等)。



我知道 std :: join 存在,但是这迫使线程等待所有线程返回。



还有另一种方法来处理吗? (例如,创建一个 ThreadPool 类而不必阻塞线程?)

解决方案

C ++ std :: thread 类实际上只是一个最小的接口,位于一些更完整的实现定义的线程包之上。这样,它仅定义了很少的功能-创建新线程,分离 join 。就是这样-没有标准的方法来管理,调度,停止/启动/杀死线程或执行其他操作。



有一个 native_handle 方法返回一个实现定义的类型,该类型可能会用于执行所需的操作,具体取决于实现。


I am trying to learn std::threads from C++11 to make a threading system.

I was wondering if there is a way to stop a thread from running (Not sleeping, but really destructing the thread or so to speak) without terminating the whole program.

I know std::join exists, but that forces a thread to wait till all threads return.

Is there another way to handle this? (For example for making a ThreadPool class without having to block a thread?)

解决方案

The C++ std::thread class is really just a minimal interface layered on top of some more complete implementation-defined threading package. As such, it only defines a tiny amount of functionality -- creating new threads, detach, and join. That's pretty much it -- there's no standard way of managing, scheduling, stopping/starting/killing threads or doing much else.

There is a native_handle method that returns an implementation-defined type which can probably be used to do what you want, depending on the implementation.

这篇关于如何在不终止程序的情况下停止运行std :: thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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