boost有interrupt()函数,可以直接打断终止,可以打断sleep。
std的没有interrupt(),只能jion,不能打断sleep。
1、std自定义线程接口(编译时包含-pthread)
包含头文件
#include<iostream>
#include<chrono>
#include <thread>
#include <mutex>
#include<atomic>
线程接口类
class i_thread
{
public:
/**
* @brief 启动线程
*/
void start()
{
_thread_flag=true;
_thread_handler=std::thread(&i_thread::thread_proc, this);
}
/**
* @brief 终止线程
*/
void stop()
{
_thread_flag=false;
_thread_handler.join();
}
///线程睡眠时间 毫秒
std::atomic<int> sleep_ms;
protected:
/// 互斥量
std::mutex _mutex;
/**
* @brief 线程函数
*/
virtual void run()=0;
private:
/// 线程句柄
std::thread _thread_handler;
/// 线程标志
std::atomic<bool> _thread_flag;
void thread_proc()
{
while(_thread_flag)
{
run();
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
}
}
};
实现类
int elapse_seconds(std::chrono::system_clock::time_point &start)
{
return std::chrono::duration_cast<std::chrono::duration<int>>
(std::chrono::system_clock::now() - start).count();
}
class thread_impl:public i_thread
{
public:
std::chrono::system_clock::time_point start_time;
std::string str;
virtual ~thread_impl(){}
protected:
void run()
{
std::cout << "get_id: "<< std::this_thread::get_id()
<< " 过了多少秒: "<< elapse_seconds(start_time)
<<" thread_impl "<< str <<std::endl;
}
};
class thread_impl2:public i_thread
{
public:
std::chrono::system_clock::time_point start_time;
std::string str;
virtual ~thread_impl2(){}
protected:
void run()
{
std::cout << "get_id: "<< std::this_thread::get_id()
<< " 过了多少秒: "<< elapse_seconds(start_time)
<<" thread_impl2 "<< str <<std::endl;
}
};
测试
int main()
{
thread_impl th1;
thread_impl2 th2;
th1.sleep_ms=1000;
th1.start_time = std::chrono::system_clock::now();
th1.str = "th1";
th2.sleep_ms=3000;
th2.start_time = std::chrono::system_clock::now();
th2.str = "th2";
th1.start();
th2.start();
std::this_thread::sleep_for(std::chrono::seconds(10));
th1.stop();
std::this_thread::sleep_for(std::chrono::seconds(10));
th2.stop();
return 0;
}
结果:
get_id: 140737337173760 过了多少秒: 0 thread_impl th1
get_id: 140737328781056 过了多少秒: 0 thread_impl2 th2
get_id: 140737337173760 过了多少秒: 1 thread_impl th1
get_id: 140737337173760 过了多少秒: 2 thread_impl th1
get_id: 140737337173760 过了多少秒: 3 thread_impl th1
get_id: 140737328781056 过了多少秒: 3 thread_impl2 th2
get_id: 140737337173760 过了多少秒: 4 thread_impl th1
get_id: 140737337173760 过了多少秒: 5 thread_impl th1
get_id: 140737337173760 过了多少秒: 6 thread_impl th1
get_id: 140737328781056 过了多少秒: 6 thread_impl2 th2
get_id: 140737337173760 过了多少秒: 7 thread_impl th1
get_id: 140737337173760 过了多少秒: 8 thread_impl th1
get_id: 140737337173760 过了多少秒: 9 thread_impl th1
get_id: 140737328781056 过了多少秒: 9 thread_impl2 th2
get_id: 140737337173760 过了多少秒: 10 thread_impl th1
get_id: 140737328781056 过了多少秒: 12 thread_impl2 th2
get_id: 140737328781056 过了多少秒: 15 thread_impl2 th2
get_id: 140737328781056 过了多少秒: 18 thread_impl2 th2
2、boost自定义线程接口
要安装boost库,包含头文件和lib,还要链接
我用到的CMakeLists.txt文件如下
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -m64 -std=c++11 -fPIC -fno-strict-aliasing")
project(test2)
#包含lib文件夹
link_directories("/root/soft/boost_1_67_0/lib")
#包含头文件夹
include_directories("." "/root/soft/boost_1_67_0/")
add_executable(${PROJECT_NAME} "main.cpp" )
#链接用到的库
target_link_libraries (${PROJECT_NAME} boost_chrono boost_system boost_thread)
包含头文件
#include<iostream>
//#include <boost/bind.hpp>
#include <boost/thread.hpp>
//#include <boost/enable_shared_from_this.hpp>
#include<chrono>
#include <thread>
#include <mutex>
#include<atomic>
自定义boost线程接口类
class i_thread//:public boost::enable_shared_from_this<i_thread>
{
public:
i_thread()
{
sleep_ms=10*1000;
}
/**
* @brief 启动线程
*/
void start()
{
//_thread_handler=boost::thread(boost::bind(&i_thread::thread_proc, shared_from_this()));//这个有问题
_thread_handler=boost::thread(&i_thread::thread_proc, this);
}
/**
* @brief 终止线程
*/
void stop()
{
_thread_flag=false;
_thread_handler.interrupt();//直接打断终止,可以打断sleep
//可以理解成1秒之后调用interrupt,但没有输出thread_interrupted
//_thread_handler.timed_join(boost::posix_time::seconds(1));
//_thread_handler.join();//不可以打断sleep,必须sleep完成后
}
///线程睡眠时间 毫秒
boost::atomic<int> sleep_ms;
protected:
/// 互斥量
std::mutex _mutex;
/**
* @brief 线程函数
*/
virtual void run(){}
private:
/// 线程句柄
boost::thread _thread_handler;
/// 线程标志
std::atomic<bool> _thread_flag;
void thread_proc()
{
try
{
while(_thread_flag)
{
run();
//不会输出 thread_interrupted
//std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
//会输出 thread_interrupted
boost::this_thread::sleep_for(boost::chrono::milliseconds(sleep_ms));
}
}
catch (boost::thread_interrupted&)
{
std::cout << "thread_interrupted" <<std::endl;
}
catch(std::exception&)
{
std::cout << "exception" <<std::endl;
}
}
};
测试
int elapse_seconds(boost::chrono::system_clock::time_point &start)
{
return boost::chrono::duration_cast<boost::chrono::duration<int>>
(boost::chrono::system_clock::now() - start).count();
}
class thread_impl:public i_thread
{
public:
std::chrono::system_clock::time_point start_time;
std::string str;
virtual ~thread_impl(){}
protected:
void run()
{
std::cout << "get_id: "<< std::this_thread::get_id()
<< " 过了多少秒: "<< elapse_seconds(start_time)
<<" thread_impl "<< str <<std::endl;
}
};
class thread_impl2:public i_thread
{
public:
std::chrono::system_clock::time_point start_time;
std::string str;
virtual ~thread_impl2(){}
protected:
void run()
{
std::cout << "get_id: "<< std::this_thread::get_id()
<< " 过了多少秒: "<< elapse_seconds(start_time)
<<" thread_impl2 "<< str <<std::endl;
}
};
int main()
{
thread_impl th1;
thread_impl2 th2;
th1.sleep_ms=10000;
th1.start_time = std::chrono::system_clock::now();
th1.str = "th1";
th2.sleep_ms=3000;
th2.start_time = std::chrono::system_clock::now();
th2.str = "th2";
th1.start();
th2.start();
boost::this_thread::sleep_for(boost::chrono::seconds(1));
th1.stop();
std::cout << "get_id: "<<std::endl;
boost::this_thread::sleep_for(boost::chrono::seconds(10));
th2.stop();
return 0;
}
结果
get_id: 140737337173760 过了多少秒: 0 thread_impl th1
get_id: 140737328781056 过了多少秒: 0 thread_impl2 th2
get_id:
thread_interrupted
get_id: 140737328781056 过了多少秒: 3 thread_impl2 th2
get_id: 140737328781056 过了多少秒: 6 thread_impl2 th2
get_id: 140737328781056 过了多少秒: 9 thread_impl2 th2