boost

一:交叉编译

  1. 执行bootstrap.sh
./bootstrap.sh
  1. 修改project-config.jam
    在这里插入图片描述
  2. 执行b2
./b2
  1. 安装
./b2 install
  1. 结果
    库文件
    在这里插入图片描述
    头文件
    在这里插入图片描述

二:功能

定时器

  1. 异步定时器
#include <iostream>  
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;  
void Print(const boost::system::error_code &ec)
{
	cout<<"Hello World!"<<endl;
	cout<<boost::this_thread::get_id()<<endl;
}
int main()
{  
	cout<<boost::this_thread::get_id()<<endl;
	boost::asio::io_service io;
	boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
 
	t.async_wait(Print);
	cout<<"to run"<<endl;
	io.run();
	cout<<"exit"<<endl;
	return 0;  
}  
  1. 同步定时器
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
 
int main()
{
  boost::asio::io_service io;
 
  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.wait();
 
  std::cout << "Hello, world!\n";
 
  return 0;
}
  1. 回调函数绑定参数
#include <iostream>  
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;  
 
 
void Print(const boost::system::error_code &ec,
			boost::asio::deadline_timer* pt,
			int * pcount)
{
	if (*pcount < 3)
	{
		cout<<"count = "<<*pcount<<endl;
		cout<<boost::this_thread::get_id()<<endl;
		(*pcount) ++;
 
		pt->expires_at(pt->expires_at() + boost::posix_time::seconds(5)) ;
 
		pt->async_wait(boost::bind(Print, boost::asio::placeholders::error, pt, pcount));
		
	}
}
int main()
{  
	cout<<boost::this_thread::get_id()<<endl;
	boost::asio::io_service io;
	boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
	int count = 0;
	t.async_wait(boost::bind(Print, boost::asio::placeholders::error, &t, &count));
	cout<<"to run"<<endl;
	io.run();
	cout << "Final count is " << count << "\n";
	cout<<"exit"<<endl;
	return 0;  
} 
  1. 多线程回调函数
#include <iostream>  
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;  
class CPrinter
{
public:
	CPrinter(boost::asio::io_service &io)
		:m_strand(io)
		,m_timer1(io, boost::posix_time::seconds(5))
		,m_timer2(io, boost::posix_time::seconds(5))
		,m_count(0)
	{
		m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this) ) );
		m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this) ) );
	}
	~CPrinter()
	{
		cout<<"m_count = "<<m_count<<endl;
	}
 
	void Print1()
	{
		if (m_count < 10)
		{
			cout<<"Timer1 count = "<<m_count<<endl;
			cout<<boost::this_thread::get_id()<<endl;
			m_count++;
			m_timer1.expires_at(m_timer1.expires_at() + boost::posix_time::seconds(1)) ;
			m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this) ) );
		}
	}
	void Print2()
	{
		if (m_count < 10)
		{
			cout<<"Timer2 count = "<<m_count<<endl;
			cout<<boost::this_thread::get_id()<<endl;
			m_count++;
			m_timer2.expires_at(m_timer2.expires_at() + boost::posix_time::seconds(1)) ;
			m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this) ) );
		}
	}
private:
	boost::asio::strand m_strand;
	boost::asio::deadline_timer m_timer1;
	boost::asio::deadline_timer m_timer2;
	int m_count;
 
};
int main()
{  
	cout<<boost::this_thread::get_id()<<endl;
	boost::asio::io_service io;
	CPrinter cp(io);
	cout<<"to run"<<endl;
	boost::thread td(boost::bind(&boost::asio::io_service::run, &io));
	io.run();
	cout<<"exit"<<endl;
	return 0;  
}  

三级目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值