原文转自 http://blog.csdn.net/anda0109/article/details/41943691

thread的成员函数interrupt()允许正在执行的线程被中断,被中断的线程会抛出一个thread_interrupted异常,它是一个空类,不是std::exception或boost::exception的子类

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/atomic.hpp> boost::mutex io_mu;//io流操作锁 void to_interrupt(boost::atomic_int &x,const std::string &str)
{
try
{
for(int i=;i<;++i)
{
boost::this_thread::sleep(boost::posix_time::seconds());//等待1s
//Sleep(1000);//等待1s
boost::mutex::scoped_lock lock(io_mu);//锁定io流操作
std::cout<<str<<++x<<std::endl;
}
}
catch (boost::thread_interrupted& )//捕获线程中断异常
{
std::cout<<"thread interrupted!"<<std::endl;
}
} int _tmain(int argc, _TCHAR* argv[])
{
boost::atomic_int x();
boost::thread t(to_interrupt,ref(x),"hello");
boost::this_thread::sleep(boost::posix_time::seconds());//休眠2s
t.interrupt();//要求线程中断执行
t.join();//由于线程已经中断,所以立即返回
getchar();
return ;
}

程序运行结果如下:

hello1
hello2
thread interrupted!

由运行结果可知,线程在执行了两次循环之后中断执行。

上面程序中使用了boost::this_thread::sleep()函数,如果换成windows API函数Sleep(1000),重新运行,则发现线程并没有终止。读者可自行试验。

这就说明线程并不是在任何时候都可以中断的。

线程中断点:

线程并非在任何时候都可以中断的,thread库定义了若干个中断点,只有当线程执行到中断点的时候才可以被中断,一个线程可以有若干个线程中断点。

thread库定义了9个中断点,它们都是函数,如下:

thread::join();

thread::timed_join();
condition_variable::wait();
condition_variable::timed_wait();
condition_variable_any::wait();
condition_variable_any::timed_wait();
thread::sleep();

this_thread::sleep();

this_thread::interruption_point();

这些中断点的前8个都是某种形式的等待函数,表明线程在阻塞的时候可以被中断。而最后一个this_thread::interruption_point();则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个地方可以被中断。

注:在xp环境下使用this_thread::sleep的时候会报错,

无法定位程序输入点GetTickCount64 在动态链接库kernel32.dll上 错误

解决方法:在stdafx.h中加#define _WIN32_WINNT 0x0501
 
 

boost::thread编程-线程中断(转)的更多相关文章

  1. C++ 系列:Boost Thread 编程指南

    转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...

  2. Thread interrupt() 线程中断的详细说明

    GitHub源码地址 原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止 ...

  3. Boost::Thread 多线程的基础知识

    Boost.Thread可以使用多线程执行可移植C++代码中的共享数据.它提供了一些类和函数来管理线程本身,还有其它一些为了实现在线程之间同步数据或者提供针对特定单个线程的数据拷贝.头文件:#incl ...

  4. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  5. 【Java并发编程】之二:线程中断

    [Java并发编程]之二:线程中断 使用interrupt()中断线程 ​ 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一 ...

  6. java并发编程(二)线程中断

    参考:http://blog.csdn.net/ns_code/article/details/17091267 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thre ...

  7. java基础知识回顾之java Thread类学习(十二)-- 线程中断

    官方文档翻译: 如果本线程是处于阻塞状态:调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态,或者调用线程的join(), join(long), ...

  8. boost::thread boost库线程

    一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...

  9. Java并发编程(2):线程中断(含代码)

    使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回.这 ...

随机推荐

  1. PHP添加扩展模块的方法

    进入源码包对应扩展模块目录下 ##extname 代表扩展模块名 cd /usr/local/src/php-5.5.36/ext/extname 然后执行phpize##phpize是一个shell ...

  2. Xenia and Bit Operations CodeForces - 339D

    Xenia and Bit Operations CodeForces - 339D Xenia the beginner programmer has a sequence a, consistin ...

  3. 动态规划:最长上升子序列(二分算法 nlogn)

    解题心得: 1.在数据量比较大的时候n^2会明显超时,所以可以使用nlogn 的算法,此算法少了双重循环,用的lower_bound(二分法). 2.lis中的数字并没有意义,仅仅是找到最小点lis[ ...

  4. 后缀数组的使用心得——POJ2774 最长连续公共子串

    对于这道题,将两个字符串直接合并成为一个字符串,分别记录连个字符串结束的位置. 首先,应用黑暗圣典的模板,我们可以顺利得到height,rank,sa三个数组. 之后直接扫描1-n所有的位置,选出来一 ...

  5. SpringCloud 微服务一:spring boot 基础项目搭建

    spring cloud是建立在spring boot的基础上的,而之前虽然听说过,也随便看了一下spring boot,却没有真正使用,因此还必须先花时间学一下spring boot. spring ...

  6. OpenCV学习笔记(十) 直方图操作

    直方图计算 直方图可以统计的不仅仅是颜色灰度, 它可以统计任何图像特征 (如 梯度, 方向等等).直方图的一些具体细节: dims: 需要统计的特征的数目, 在上例中, dims = 1 因为我们仅仅 ...

  7. 8 django 里面的API

    1.什么是API? 2.在djang里面写API 3.API实战效果 1.移动端的网页 4.restframework :老师方法 (0)安装 Django REST framework 是一个强大且 ...

  8. Install ADDS on Windows Server 2012 R2 with PowerShell

    Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...

  9. Python+Selenium练习篇之8-利用css定位元素

    前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...

  10. c++ primer plus 第6版 部分一 1-4章

    c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c   cc   cxx     C    cpp     ...