【Boost】boost库中thread多线程详解5——谈谈线程中断
线程不是在任意时刻都可以被中断的。如果将线程中函数中的sleep()睡眠等待去掉,那么即使在主线程中调用interrupt()线程也不会被中断。thread库预定义了若干个线程的中断点,只有当线程执行到中断点的时候才能被中断,一个线程可以拥有任意多个中断点。
thread库预定义了共9个中断点,它们都是函数,如下:
1. thread::join();
2. thread::timed_join();
3. condition_variable::wait();
4. condition_variable::timed_wait();
5. condition_variable_any::wait();
6. condition_variable_any::timed_wait();
7. thread::sleep();
8. this_thread::sleep();
9. this_thread::interruption_point()
这些中断点中的前8个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。 而最后一个位于子名字空间this_thread的interruption_point()则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个函数所在的语句就可以被中断。看看下面的例子:
- namespace
- {
- boost::mutex io_mu;
- void to_interrupt(const std::string& str)
- {
- // 如果在线程外部调用了this_thread->interrupt()
- // 线程内部的以下这些检查点可以抛出boost::thread_interrupted异常
- try
- {
- boost::this_thread::disable_interruption();
- for (int i = 0; i < 5; ++i)
- {
- boost::mutex::scoped_lock lock(io_mu);
- PRINT_DEBUG(i);
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- // PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_requested());
- if (i == 2)
- {
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- boost::this_thread::interruption_point();
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- }
- }
- }
- catch (boost::thread_interrupted &)
- {
- }
- }
- }
- void test_thread_interrupt()
- {
- boost::thread t(to_interrupt, "hello");
- // 中断函数
- t.interrupt();
- t.join();
- }
运行结果:
- 2013-01-02 11:00:44 263 [8272] DEBUG - 0
- 2013-01-02 11:00:44 266 [8272] DEBUG - 1
- 2013-01-02 11:00:44 269 [8272] DEBUG - 2
如果注释boost::this_thread::interrupt_point了,则结果如下:
- 2013-01-02 11:02:06 555 [5168] DEBUG - 0
- 2013-01-02 11:02:06 559 [5168] DEBUG - 1
- 2013-01-02 11:02:06 561 [5168] DEBUG - 2
- 2013-01-02 11:02:06 564 [5168] DEBUG - 3
- 2013-01-02 11:02:06 567 [5168] DEBUG - 4
下面谈谈启用/禁用线程中断
缺省情况下钱程都是允许中断的,但thread库允许控制线程的中断行为。
thread 库在子名字空间this_thread提供了一组函数和类来共同完成线程的中断启用和禁用:
1. interruption_enabled(): 函数检测当前线程是否允许中断
2. interruption_requested(): 函数检测当前线程是否被要求中断
3. 类disable_interruption是一个RAII类型的对象,它在构造时关闭线程的中断,析构时自动恢复线程的中断状态。在disable_interruption 的生命期内线程始终是不可中断的,除非使用了restore_interruption 对象。
4. restore_interruption只能在disable_interruption 的作用域内使用,它在构造时临时打开线程的中断状态,在析构时又关闭中断状态。
这些中断点中的前8个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。
- namespace
- {
- boost::mutex io_mu;
- void to_interrupt_disable(const std::string& str)
- {
- // 默认可以中断
- assert(boost::this_thread::interruption_enabled());
- for (int i = 0; i < 10; i++)
- {
- // 关闭中断
- boost::this_thread::disable_interruption di;
- // 此时中断不可用
- PRINT_DEBUG(std::boolalpha << "interruption_enabled = " << boost::this_thread::interruption_enabled());
- // 是否有中断请求
- PRINT_DEBUG(std::boolalpha << "interruption_requested = " << boost::this_thread::interruption_requested());
- boost::mutex::scoped_lock lock(io_mu);
- PRINT_DEBUG(i);
- // 使用中断点函数,因为关闭中断,此时无效果。 中断恢复后,它才生效。
- boost::this_thread::interruption_point();
- if (i == 8)
- {
- // 临时恢复中断
- boost::this_thread::restore_interruption ri(di);
- PRINT_DEBUG(std::boolalpha << "interruption_enabled = " << boost::this_thread::interruption_enabled());
- PRINT_DEBUG(std::boolalpha << "interruption_enabled after restore = " << boost::this_thread::interruption_enabled());
- boost::this_thread::interruption_point();
- }
- }
- }
- }
- void test_thread_interrupt_disable()
- {
- boost::thread t(to_interrupt_disable, "hello");
- t.interrupt();
- t.join();
- }
结果:
- 2013-01-02 14:09:35 538 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 544 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 551 [7628] DEBUG - 0
- 2013-01-02 14:09:35 555 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 563 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 570 [7628] DEBUG - 1
- 2013-01-02 14:09:35 574 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 581 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 586 [7628] DEBUG - 2
- 2013-01-02 14:09:35 589 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 601 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 608 [7628] DEBUG - 3
- 2013-01-02 14:09:35 614 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 621 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 627 [7628] DEBUG - 4
- 2013-01-02 14:09:35 630 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 637 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 643 [7628] DEBUG - 5
- 2013-01-02 14:09:35 646 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 650 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 655 [7628] DEBUG - 6
- 2013-01-02 14:09:35 659 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 663 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 667 [7628] DEBUG - 7
- 2013-01-02 14:09:35 670 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 679 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 685 [7628] DEBUG - 8
- 2013-01-02 14:09:35 689 [7628] DEBUG - interruption_enabled = true
- 2013-01-02 14:09:35 695 [7628] DEBUG - interruption_enabled after restore = true
可以通过thread对象的interrupt函数,通知线程,需要interrupt。线程运行到interruption point就可以退出。
Interruption机制举例:
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
using namespace std;void f()
{for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()="<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::interruption_point();}}}
}int _tmain(int argc, _TCHAR* argv[])
{boost::thread t(f);t.interrupt();t.join(); //等待线程结束return 0;
}
t.interrupt();告诉t线程,现在需要interrupt。boost::this_thread::interruption_requested()可以得到当前线程是否有一个interrupt请求。若有interrupt请求,线程在运行至interruption点时会结束。boost::this_thread::interruption_point();就是一个interruption point。Interruption point有多种形式,较常用的有boost::this_thread::sleep(boost::posix_time::seconds(5));当没有interrupt请求时,这条语句会让当前线程sleep五秒,若有interrupt requirement线程结束。
如何使线程在运行到interruption point的时候,不会结束,可以参考下面的例子:
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
using namespace std;void f()
{for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()"<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::disable_interruption di;{boost::this_thread::interruption_point();}}}}
}int _tmain(int argc, _TCHAR* argv[])
{boost::thread t(f);t.interrupt();t.join(); //等待线程结束return 0;
}
注意boost::this_thread::disable_interruption这条语句的使用,它可以使大括号内的interruption point不会中断当前线程。
【Boost】boost库中thread多线程详解5——谈谈线程中断相关推荐
- 【Boost】boost库中thread多线程详解9——thread_specific_ptr线程局部存储
大多数函数都不是可重入的.这也就是说在某一个线程已经调用了一个函数时,如果你再调用同一个函数,那么这样是不安全的.一个不可重入的函数通过连续的调用来保存静态变量或者是返回一个指向静态数据的指针. 举例 ...
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex
如果一个线程中可能在执行中需要再次获得锁的情况(例子:test_thread_deadlock),按常规的做法会出现死锁. 此时就需要使用递归式互斥量boost::recursive_mutex,例子 ...
- 【Boost】boost库中thread多线程详解1——thread入门与简介
1. 概述 线程就是,在同一程序同一时间内允许执行不同函数的离散处理队列. 这使得一个长时间去进行某种特殊运算的函数在执行时不阻碍其他的函数变得十分重要. 线程实际上允许同时执行两种函数,而这两个函数 ...
- 【Boost】boost库中thread多线程详解8——call_once仅运行一次
还有一个问题没有解决:如何使得初始化工作(比如说构造函数)也是线程安全的.比方说,如果一个引用程序要产生唯一的全局的对象,由于实例化顺序的问题,某个函数会被调用来返回一个静态的对象,它必须保证第一次被 ...
- 【Boost】boost库中thread多线程详解13——线程标识符
在boost中也有唯一标识线程的数据结构:thread::id. boost::thread thread_func(func); thread::id var_id = thread_func.ge ...
- 【Boost】boost库中thread多线程详解12——线程的分离与非分离
Boos::thread线程的默认属性为非分离状态,线程结束后线程标识符.线程退出状态等信息需要通过join方法回收. boost::thread thread_func(func); thread_ ...
- 【Boost】boost库中thread多线程详解11——线程的休眠和中断
boost::thread 中提供一个静态方法 void boost::thread::sleep(system_time const& abs_time); 线程将休眠直到时间超时. sle ...
- 【Boost】boost库中thread多线程详解10——condition条件变量
有的时候仅仅依靠锁住共享资源来使用它是不够的.有时候共享资源只有某些状态的时候才能够使用.比方说,某个线程如果要从堆栈中读取数据,那么如果栈中没有数据就必须等待数据被压栈.这种情况下的同步使用互斥体是 ...
- 【Boost】boost库中thread多线程详解6——线程组简单例子
如果你需要创建几个线程,考虑使用一个线程组对象thread_group来组织它们.一个thread_group对象可以使用多种方法管理线程.首先,可以使用一个指向动态创建的线程对象的指针作为参数来调用 ...
最新文章
- 如何查看dll被那个service占用_不小心执行 rm -f,该如何恢复?
- Cordova创建你的第一个App
- 阴差阳错2019-12-13
- 中医科学院临基所携手第四范式助力抗疫工作
- 使用Custom.pll修改标准Form的LOV
- SAP Spartacus 数据类型定义汇总
- leetcode1337. 方阵中战斗力最弱的 K 行(优先队列)
- JVectorMap 实现中国地图
- 查看硬件配置的Linux命令,LINUX 查看硬件配置命令的教程
- 一文带你掌握Redis操作指南
- ue4渲染速度太慢_推介飞向月球纪录片基于Unreal实时渲染引擎的三维流程化制作...
- java一维数组的使用_Java初步认知和使用一维数组
- 【渝粤教育】国家开放大学2018年春季 0631-21T动物常见病防治 参考试题
- MATS-2010H硬磁测量装置产品详细介绍
- 计算机病毒有几个阶段,计算机病毒发展9阶段
- Lobooi个人作业:阅读与准备作业
- python实现随机抽奖游戏
- 聊天室项目开发过程总结
- 创业起步阶段需要注意的几点
- 2018年 吉林大学 软件工程 967考研经验分享