线程不是在任意时刻都可以被中断的。如果将线程中函数中的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()则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个函数所在的语句就可以被中断。看看下面的例子:

[cpp] view plaincopy print?
  1. namespace
  2. {
  3. boost::mutex io_mu;
  4. void to_interrupt(const std::string& str)
  5. {
  6. // 如果在线程外部调用了this_thread->interrupt()
  7. // 线程内部的以下这些检查点可以抛出boost::thread_interrupted异常
  8. try
  9. {
  10. boost::this_thread::disable_interruption();
  11. for (int i = 0; i < 5; ++i)
  12. {
  13. boost::mutex::scoped_lock lock(io_mu);
  14. PRINT_DEBUG(i);
  15. PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
  16. // PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_requested());
  17. if (i == 2)
  18. {
  19. PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
  20. boost::this_thread::interruption_point();
  21. PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
  22. }
  23. }
  24. }
  25. catch (boost::thread_interrupted &)
  26. {
  27. }
  28. }
  29. }
  30. void test_thread_interrupt()
  31. {
  32. boost::thread t(to_interrupt, "hello");
  33. // 中断函数
  34. t.interrupt();
  35. t.join();
  36. }

运行结果:

[cpp] view plaincopy print?
  1. 2013-01-02 11:00:44 263 [8272] DEBUG - 0
  2. 2013-01-02 11:00:44 266 [8272] DEBUG - 1
  3. 2013-01-02 11:00:44 269 [8272] DEBUG - 2

如果注释boost::this_thread::interrupt_point了,则结果如下:

[cpp] view plaincopy print?
  1. 2013-01-02 11:02:06 555 [5168] DEBUG - 0
  2. 2013-01-02 11:02:06 559 [5168] DEBUG - 1
  3. 2013-01-02 11:02:06 561 [5168] DEBUG - 2
  4. 2013-01-02 11:02:06 564 [5168] DEBUG - 3
  5. 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个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。

[cpp] view plaincopy print?
  1. namespace
  2. {
  3. boost::mutex io_mu;
  4. void to_interrupt_disable(const std::string& str)
  5. {
  6. // 默认可以中断
  7. assert(boost::this_thread::interruption_enabled());
  8. for (int i = 0; i < 10; i++)
  9. {
  10. // 关闭中断
  11. boost::this_thread::disable_interruption di;
  12. // 此时中断不可用
  13. PRINT_DEBUG(std::boolalpha << "interruption_enabled = " <<  boost::this_thread::interruption_enabled());
  14. // 是否有中断请求
  15. PRINT_DEBUG(std::boolalpha << "interruption_requested = " <<  boost::this_thread::interruption_requested());
  16. boost::mutex::scoped_lock lock(io_mu);
  17. PRINT_DEBUG(i);
  18. // 使用中断点函数,因为关闭中断,此时无效果。 中断恢复后,它才生效。
  19. boost::this_thread::interruption_point();
  20. if (i == 8)
  21. {
  22. // 临时恢复中断
  23. boost::this_thread::restore_interruption ri(di);
  24. PRINT_DEBUG(std::boolalpha << "interruption_enabled = " <<  boost::this_thread::interruption_enabled());
  25. PRINT_DEBUG(std::boolalpha << "interruption_enabled after restore = " <<  boost::this_thread::interruption_enabled());
  26. boost::this_thread::interruption_point();
  27. }
  28. }
  29. }
  30. }
  31. void test_thread_interrupt_disable()
  32. {
  33. boost::thread t(to_interrupt_disable, "hello");
  34. t.interrupt();
  35. t.join();
  36. }

结果:

[cpp] view plaincopy print?
  1. 2013-01-02 14:09:35 538 [7628] DEBUG - interruption_enabled = false
  2. 2013-01-02 14:09:35 544 [7628] DEBUG - interruption_requested = true
  3. 2013-01-02 14:09:35 551 [7628] DEBUG - 0
  4. 2013-01-02 14:09:35 555 [7628] DEBUG - interruption_enabled = false
  5. 2013-01-02 14:09:35 563 [7628] DEBUG - interruption_requested = true
  6. 2013-01-02 14:09:35 570 [7628] DEBUG - 1
  7. 2013-01-02 14:09:35 574 [7628] DEBUG - interruption_enabled = false
  8. 2013-01-02 14:09:35 581 [7628] DEBUG - interruption_requested = true
  9. 2013-01-02 14:09:35 586 [7628] DEBUG - 2
  10. 2013-01-02 14:09:35 589 [7628] DEBUG - interruption_enabled = false
  11. 2013-01-02 14:09:35 601 [7628] DEBUG - interruption_requested = true
  12. 2013-01-02 14:09:35 608 [7628] DEBUG - 3
  13. 2013-01-02 14:09:35 614 [7628] DEBUG - interruption_enabled = false
  14. 2013-01-02 14:09:35 621 [7628] DEBUG - interruption_requested = true
  15. 2013-01-02 14:09:35 627 [7628] DEBUG - 4
  16. 2013-01-02 14:09:35 630 [7628] DEBUG - interruption_enabled = false
  17. 2013-01-02 14:09:35 637 [7628] DEBUG - interruption_requested = true
  18. 2013-01-02 14:09:35 643 [7628] DEBUG - 5
  19. 2013-01-02 14:09:35 646 [7628] DEBUG - interruption_enabled = false
  20. 2013-01-02 14:09:35 650 [7628] DEBUG - interruption_requested = true
  21. 2013-01-02 14:09:35 655 [7628] DEBUG - 6
  22. 2013-01-02 14:09:35 659 [7628] DEBUG - interruption_enabled = false
  23. 2013-01-02 14:09:35 663 [7628] DEBUG - interruption_requested = true
  24. 2013-01-02 14:09:35 667 [7628] DEBUG - 7
  25. 2013-01-02 14:09:35 670 [7628] DEBUG - interruption_enabled = false
  26. 2013-01-02 14:09:35 679 [7628] DEBUG - interruption_requested = true
  27. 2013-01-02 14:09:35 685 [7628] DEBUG - 8
  28. 2013-01-02 14:09:35 689 [7628] DEBUG - interruption_enabled = true
  29. 2013-01-02 14:09:35 695 [7628] DEBUG - interruption_enabled after restore = true
Interruption机制:
可以通过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——谈谈线程中断相关推荐

  1. 【Boost】boost库中thread多线程详解9——thread_specific_ptr线程局部存储

    大多数函数都不是可重入的.这也就是说在某一个线程已经调用了一个函数时,如果你再调用同一个函数,那么这样是不安全的.一个不可重入的函数通过连续的调用来保存静态变量或者是返回一个指向静态数据的指针. 举例 ...

  2. 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex

    如果一个线程中可能在执行中需要再次获得锁的情况(例子:test_thread_deadlock),按常规的做法会出现死锁. 此时就需要使用递归式互斥量boost::recursive_mutex,例子 ...

  3. 【Boost】boost库中thread多线程详解1——thread入门与简介

    1. 概述 线程就是,在同一程序同一时间内允许执行不同函数的离散处理队列. 这使得一个长时间去进行某种特殊运算的函数在执行时不阻碍其他的函数变得十分重要. 线程实际上允许同时执行两种函数,而这两个函数 ...

  4. 【Boost】boost库中thread多线程详解8——call_once仅运行一次

    还有一个问题没有解决:如何使得初始化工作(比如说构造函数)也是线程安全的.比方说,如果一个引用程序要产生唯一的全局的对象,由于实例化顺序的问题,某个函数会被调用来返回一个静态的对象,它必须保证第一次被 ...

  5. 【Boost】boost库中thread多线程详解13——线程标识符

    在boost中也有唯一标识线程的数据结构:thread::id. boost::thread thread_func(func); thread::id var_id = thread_func.ge ...

  6. 【Boost】boost库中thread多线程详解12——线程的分离与非分离

    Boos::thread线程的默认属性为非分离状态,线程结束后线程标识符.线程退出状态等信息需要通过join方法回收. boost::thread thread_func(func); thread_ ...

  7. 【Boost】boost库中thread多线程详解11——线程的休眠和中断

    boost::thread 中提供一个静态方法 void boost::thread::sleep(system_time const& abs_time); 线程将休眠直到时间超时. sle ...

  8. 【Boost】boost库中thread多线程详解10——condition条件变量

    有的时候仅仅依靠锁住共享资源来使用它是不够的.有时候共享资源只有某些状态的时候才能够使用.比方说,某个线程如果要从堆栈中读取数据,那么如果栈中没有数据就必须等待数据被压栈.这种情况下的同步使用互斥体是 ...

  9. 【Boost】boost库中thread多线程详解6——线程组简单例子

    如果你需要创建几个线程,考虑使用一个线程组对象thread_group来组织它们.一个thread_group对象可以使用多种方法管理线程.首先,可以使用一个指向动态创建的线程对象的指针作为参数来调用 ...

最新文章

  1. 如何查看dll被那个service占用_不小心执行 rm -f,该如何恢复?
  2. Cordova创建你的第一个App
  3. 阴差阳错2019-12-13
  4. 中医科学院临基所携手第四范式助力抗疫工作
  5. 使用Custom.pll修改标准Form的LOV
  6. SAP Spartacus 数据类型定义汇总
  7. leetcode1337. 方阵中战斗力最弱的 K 行(优先队列)
  8. JVectorMap 实现中国地图
  9. 查看硬件配置的Linux命令,LINUX 查看硬件配置命令的教程
  10. 一文带你掌握Redis操作指南
  11. ue4渲染速度太慢_推介飞向月球纪录片基于Unreal实时渲染引擎的三维流程化制作...
  12. java一维数组的使用_Java初步认知和使用一维数组
  13. 【渝粤教育】国家开放大学2018年春季 0631-21T动物常见病防治 参考试题
  14. MATS-2010H硬磁测量装置产品详细介绍
  15. 计算机病毒有几个阶段,计算机病毒发展9阶段
  16. Lobooi个人作业:阅读与准备作业
  17. python实现随机抽奖游戏
  18. 聊天室项目开发过程总结
  19. 创业起步阶段需要注意的几点
  20. 2018年 吉林大学 软件工程 967考研经验分享

热门文章

  1. Calendar类的常用成员方法
  2. Eureka的服务自我保护
  3. SpringBoot 逻辑判断
  4. JVM--对象的实例化过程
  5. java reader类 实例_Java Reader ready()用法及代码示例
  6. python参数默认值实例_Python中使用partial改变方法默认参数实例
  7. 我的前端工具集(四)树状结构后篇
  8. 20155301实验三 免杀原理与实践
  9. 浅谈EntityFramework框架的使用
  10. lua cocos 创建动画的几种方式