使用boost线程定时器作为后台线程来切换主循环程序状态方法总结

  1:简单了解boost定时器

#include "stdafx.h"
#include <string>
#include <boost	hread.hpp>
#include <boost/asio.hpp>
#include <iostream>
using namespace boost::asio;
using namespace boost;
int exit1 = 1;
void printing(int& len)
{
    this_thread::sleep_for(chrono::seconds(len));//线程中阻塞定时后改变系统状态
    len++;
    exit1 = 0;
}
int _tmain(int argc, _TCHAR* argv[])
{    
    //绑定简单函数
    int len = 2;
    thread(printing, len);//默认构造函数都是复制值传递,如果要使用引用传递请使用
    //thread(printing, ref(len));
    while (exit1);//线程中定时后改变系统阻塞的状态;
    printf("ddd");
    getchar();
    return 0;
}
View Code


 2:使用bind方法

 1 #include <string>
 2 #include <boost	hread.hpp>
 3 #include <boost/asio.hpp>
 4 #include <iostream>
 5 using namespace boost::asio;
 6 using namespace boost;
 7 int exit1 = 1;
 8 void printing(int& len, const char * str)
 9 {        
10     printf("%s", str);
11     this_thread::sleep_for(chrono::seconds(len));//线程中阻塞定时后改变系统状态
12     len++;
13 
14     exit1 = 0;
15 }
16 
17 int _tmain(int argc, _TCHAR* argv[])
18 {
19     thread(bind(printing, 2, "thread runing"));//使用bind函数将函数绑定为一个函数对象
20     while (exit1);//线程中定时后改变系统阻塞的状态;
21     printf("ddd");
22     getchar();
23     return 0;
24 }
View Code

3:使用类的处理方式(核心哦)

说明:(1)前面两种主要是基本使用方法,而方法3已经用在了实际工程开发中,方法3主要涉及的一种状态转化的框架,在每种状态下,都有一个while死循环的进行网络或者通信口的数据读写,当满足某种条件时,进行状态的跳转;while是阻塞而线程定时器则作为后台进行状态的转化;

(2)本文的线程定时器应该叫做线程延时器,主要是在后台运行;线程成为工作者函数或者程序工作者方法;线程中延时结束后,会调用本类的成员函数作为回调处理;

(3)如果某个状态不更新,则只需要在回调中再次进行开启线程即可;

(4)当然建议使用Bind方式绑定工作者方法,否则就要设置工作函数为静态方法,还得使用reinterpret_cast强制转换指针;

(5)关于reinterpret_cast,我的理解就是"能将this指针参数强制转化为this",从而达到可以成员函数的目的;

(6)最关键的所有的操作使用的boost,本文用的编译工具是VS2013;

/*线程定时器在类中使用,需要注意的是:
(1)此时必须使用“绑定成员函数”的方式给thread传入工作者线程方法,否则不能编译通过,使用这种方法好处在于工作者线程不必是静态函数;
(2)如果不采用bind方式,则必须需要将“工作者线程设置为静态函数”,另加入this指针,使用这种方法比较this指针获取比较难理解,并且工作者函数必须是静态函数;

*/


#include "stdafx.h"
#include <string>
#include <boost	hread.hpp>
#include <boost/asio.hpp>
#include <iostream>
using namespace boost::asio;
using namespace boost;

enum program_state
{
	state1,
	state2,
	state3,
	state4
};
class TestClassA
{
public:
	TestClassA()
	{
		state = state1;
	}
	program_state state;
	void  TimeoutPrint(int& len, const char * str);
	void  TimeoutCallbackPrint();
	void run();
	void proc_state1();
	void proc_state2();
	void static StaticTimeoutPrint(int& len, const char * str,void* This);


};
void TestClassA::TimeoutPrint(int& len, const char * str)
{
	printf("%s in
", str);
	this_thread::sleep_for(chrono::seconds(len));//线程中阻塞定时后改变系统状态
	printf("%s out
", str);
	//直接使用this指针就能调用回调函数
	this->TimeoutCallbackPrint();
}
void TestClassA::TimeoutCallbackPrint()
{
	this->state = state2;
	printf(" TimeoutCallbackPrint
");
}
void TestClassA::proc_state1()
{	
	//采用绑定成员函数的方式,注意绑定成员函数时,需要一个占位符,提供类实例、引用或者指针
	thread(bind(&TestClassA::TimeoutPrint,this, 5, "thread 1"));
	while (this->state == state1)
	{
	}
}
void TestClassA::StaticTimeoutPrint(int& len, const char * str,void* This)
{
	printf("%s in
", str);
	this_thread::sleep_for(chrono::seconds(len));//线程中阻塞定时后改变系统状态
	printf("%s out
", str);
	//this指针需要强制转换才能访问成员函数
	((reinterpret_cast<TestClassA*>(This)))->TimeoutCallbackPrint();
}
void TestClassA::proc_state2()
{
	//采用静态函数方式
	thread(TestClassA::StaticTimeoutPrint, 5, "thread 2",this);
	while (this->state == state2)
	{
	}
}
void TestClassA::run()
{
	while (1)
	{
		switch (this->state)
		{
			case state1:
				proc_state1();
				break;
			case state2:
				proc_state2(); break;
		}

	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	TestClassA *tc = new TestClassA();
	tc->run();
	return 0;
}
原文地址:https://www.cnblogs.com/cdma/p/4479028.html