线程不是在任意时刻都可以被中断的。如果将线程中函数中的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. wpf 绑定数据无法更新ui控件可能存在的问题
  2. Libusb交叉编译和移植
  3. QT的QCommandLineOption类的使用
  4. springboot之数据校验及多环境切换
  5. java io 机器名_java IO最让初学者误解的取名方式
  6. 使用dao时,如何同时使用动态表名和过滤字段?
  7. python+Eclipse+pydev环境搭建
  8. 嵌入式MicroFlighter 之STM32F103学习——编写第一个STM32程序
  9. 难过的时候看一看 再坏能有多坏!
  10. Cisco 交换机配置文件存放位置及管理要点
  11. Echarts数据可视化特效散点图点动态闪烁效果
  12. 论文相关-论文写作-图片色卡
  13. python拼写检查_python 英语单词拼写检查算法
  14. PYTHON开发对接短信语音验证码接口
  15. java全角数字_Java全角、半角字符的关系以及转换
  16. 解决Failing package is:mysql-community-icu-data-files-8.0.29-1.el7.x86_64 GPG Keys are configured as..
  17. 四、s3c2440 裸机开发 通用异步收发器UARN
  18. 学术论文摘要写作技巧:
  19. Hive操作——删除表(drop、truncate)
  20. 盖茨被逐出微软董事会真相曝光:长期跟员工搞地下情,27年婚姻中出轨不断,人设已崩...

热门文章

  1. OAuth2.0授权流程分析
  2. Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches
  3. Request_继承体系
  4. 字符串的转换相关方法
  5. SpringBoot_配置-@PropertySource、@ImportResource、@Bean
  6. Mockito详细介绍
  7. jar包不统一也会报错:Exception in thread main java.lang.NoClassDefFoundError
  8. android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码...
  9. 学习新 api 的思考过程 4.18
  10. 使用git推送代码到开源中国以及IDEA环境下使用git