boost::thread编程-线程中断(转)
原文转自 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上 错误
boost::thread编程-线程中断(转)的更多相关文章
- C++ 系列:Boost Thread 编程指南
转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...
- Thread interrupt() 线程中断的详细说明
GitHub源码地址 原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止 ...
- Boost::Thread 多线程的基础知识
Boost.Thread可以使用多线程执行可移植C++代码中的共享数据.它提供了一些类和函数来管理线程本身,还有其它一些为了实现在线程之间同步数据或者提供针对特定单个线程的数据拷贝.头文件:#incl ...
- Boost::thread库的使用
阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...
- 【Java并发编程】之二:线程中断
[Java并发编程]之二:线程中断 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一 ...
- java并发编程(二)线程中断
参考:http://blog.csdn.net/ns_code/article/details/17091267 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thre ...
- java基础知识回顾之java Thread类学习(十二)-- 线程中断
官方文档翻译: 如果本线程是处于阻塞状态:调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态,或者调用线程的join(), join(long), ...
- boost::thread boost库线程
一.boost::thread的创建 1.线程创建方法一: boost::shared_ptr<boost::thread> writeThread_; boost::function0& ...
- Java并发编程(2):线程中断(含代码)
使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回.这 ...
随机推荐
- Linux-git安装
基本操作 安装yum install git 生成SSH KEY :先cd ~/.ssh,在这个目录下输入ssh-keygen,一直回车就可以了,这个时候就会出现id_rsd.pub公钥和id_rsa ...
- 两台centos之间使用scp命令传输文件
1.将本地文件copy到远程 (1)复制文件 scp local_file remote_username@remote_ip:remote_folder 例如:将/usr/local/aa.png文 ...
- composer 类加载器,对 <PSR-4的风格>、<PSR-0的风格>、<PEAR的风格> 风格的类的加载
class ClassLoader { // ... /** * composer 类加载器,对 <PSR-4的风格>.<PSR-0的风格>.<PEAR的风格> 风 ...
- 微信小程序 input组件type属性3个值的作用
input组件是小程序的内容输入框组件,通常是这样来使用的: <input type="text" placeholder="输入点内容吧" /> ...
- oracle 事务 锁机制
原文地址:http://www.cnblogs.com/quanweiru/archive/2013/05/24/3097367.html 本课内容属于Oracle高级课程范畴,内容略微偏向理论性,但 ...
- Susan Sontag【苏珊·桑塔格】
Sunsan Sontag Sunsan Sontag was one of the most noticeable figures in the world of literature. 苏珊·桑塔 ...
- printf("\033[1;33m ***** \033[0m \n");
printf("\033[1;33m Hello World. \033[0m \n"); 颜色如下: none = "\033[0m" black = &qu ...
- mac terminal基本命令
文件目录 首先要清楚几个文件目录: " / " :根目录 " ~ " :用户主目录的缩写.例如当前用户为esther,那么" ~ "展开来 ...
- Java8_Lambda表达式
从2014年java8发布到现在已经有几个年头了,现在java11都发布了.公司最近把服务器环境重新搭建了一遍,jdk版本也从7换成了8,终于可以在代码里面写Lambda表达式了.作为 ...
- Android资源限定符
Android系统会根据设备参数,自动选择最佳资源配置方案. Android中常见的资源限定符: 屏幕特征 限定符 描述 大小 small 提供给小屏幕设备的资源 normal 提供给中等屏幕设备的资 ...