大学生做网站类型,石家庄网站建设平台,sem投放,江苏省建设局网站首页提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pthread_cancel1.代码演示2.两个重要方法1.pthread_setcancelstate2.pthread_setcanceltype 3.资源回收 二、Boost1.看代码2.资源泄露2.资源回收 总结 前言… 提示文章写完后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 前言一、pthread_cancel1.代码演示2.两个重要方法1.pthread_setcancelstate2.pthread_setcanceltype 3.资源回收 二、Boost1.看代码2.资源泄露2.资源回收 总结 前言
我们都知道在C11中可以方便启动一个或多个线程常规的手段是让线程执行完任务后自己结束自己或者在达成一定的条件时退出。如果我想在运行途中停下来怎么办这篇文章就提供几种可行的方法。
取消点线程并不是所有时刻都可以打断只有当线程到达取消点的时候才可能被取消通俗来说就是阻塞。诸如join、wait、sleep、IO操作都是典型的取消点。 一、pthread_cancel
这个是C形式的线程取消方式搭配pthread_create方式创建的线程使用。
1.代码演示
main.cpp
#include iostream
using namespace std;void *p_(void *) {printf(start\n);for (int i 0; i 100000; i) {if (i 1000)printf(block\n);}printf(end\n);return nullptr;
}void pthread_() {pthread_t p;pthread_create(p, nullptr, p_, nullptr);pthread_cancel(p);pthread_join(p, nullptr);
}int main() {pthread_();return 0;
}CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(Thread)set(CMAKE_CXX_STANDARD 11)
find_package(Threads REQUIRED)
add_executable(Thread main.cpp)
target_link_libraries(Thread Threads::Threads)打印start 说明程序正好在printf(start\n);这句被结束掉了。如果你将主线程sleep下就可以执行到printf(block\n);。printf(end\n);这句没有执行到也正好符合终止线程的意图。
2.两个重要方法
pthread_cancel还有两个重要方法搭配使用pthread_setcancelstate和pthread_setcanceltype。
1.pthread_setcancelstate
设置线程对cancel信号的响应策略有两个可选项PTHREAD_CANCEL_ENABLE和PTHREAD_CANCEL_DISABLE前者是默认的选项表示响应pthread_cancel的调用并设置为cancel状态后者是说线程忽略信号调用pthread_cancel的线程阻塞到可取消状态为止。
2.pthread_setcanceltype
设置线程对cancel信号的返回类型也有两个可选项PTHREAD_CANCEL_DEFERRED和PTHREAD_CANCEL_ASYNCHRONOUS前者是默认状态表示线程运行到下一个取消点才退出后者意思是直接退出不用等线程运行到下一个取消点。
注意pthread_setcanceltype设置必须先将pthread_setcancelstate设置为enable状态才会生效
3.资源回收
直接执行pthread_cancel会引发资源泄露的问题请看代码
#include iostream
using namespace std;void *p_(void *) {auto a new int{1};printf(start\n);for (int i 0; i 100000; i) {if (i 1000)printf(block\n);}printf(end\n);return nullptr;
}void pthread_() {pthread_t p;pthread_create(p, nullptr, p_, nullptr);pthread_cancel(p);pthread_join(p, nullptr);
}int main() {pthread_();return 0;
}使用valgrind测试发现4字节的内存泄露。
资源回收的方法是有的但是我更推荐使用Boost的方法所以这里不深究了感兴趣的请自行研究。
二、Boost
相比于pthread我觉得Boost的thread更好用。典型的C书写方式而且方法少而简单。只是不知道为什么C11标准里没有interrupt这个函数留待后续研究使用起来也没发现什么问题毕竟需要打断线程的场景其实不多。
1.看代码
main.cpp
#include iostream
#include boost/thread.hppvoid thread_() {try {for (int i 0; i 100000; i) {std::cout i std::endl;boost::this_thread::sleep(boost::posix_time::seconds(1));}} catch (boost::thread_interrupted) {std::cout interrupted std::endl;}
}int main(int argc, char **argv) {boost::thread t(thread_);t.interrupt();t.join();return 0;
}CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(Boost_1_74_0)set(CMAKE_CXX_STANDARD 11)
find_package(Boost 1.74 REQUIRED COMPONENTS thread)
add_executable(thread thread.cpp)
target_link_libraries(thread Boost::thread)
执行 0 interrupted
触发了异常之后结束了。
2.资源泄露
看代码
#include iostream
#include boost/thread.hppvoid thread_() {auto a new int{1};try {for (int i 0; i 100000; i) {std::cout i std::endl;boost::this_thread::sleep(boost::posix_time::seconds(1));}} catch (boost::thread_interrupted) {std::cout interrupted std::endl;}
}int main(int argc, char **argv) {boost::thread t(thread_);t.interrupt();t.join();return 0;
}运行valgrind等待程序结束。
2.资源回收
方法很简单Boost对interrupt做了异常包装触发异常之后直接回收资源就行了。 看代码
#include iostream
#include boost/thread.hppvoid thread_() {auto a new int{1};try {for (int i 0; i 100000; i) {std::cout i std::endl;boost::this_thread::sleep(boost::posix_time::seconds(1));}} catch (boost::thread_interrupted) {std::cout interrupted std::endl;delete a;//这一句资源回收}
}int main(int argc, char **argv) {boost::thread t(thread_);t.interrupt();t.join();return 0;
}运行valgrind等待程序结束。所有资源都被回收了。 总结
1、优先使用Boost的方法没别的原因就是简单。 2、当然线程自身也可以打断自己只不过我一般选择标志或自动结束更简单些。 文章转载自: http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.mgkb.cn.gov.cn.mgkb.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.cprbp.cn.gov.cn.cprbp.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn