一:交叉编译
- 执行
bootstrap.sh
./bootstrap.sh
- 修改
project-config.jam

- 执行
b2
./b2
- 安装
./b2 install
- 结果
库文件

头文件

二:功能
定时器
- 异步定时器
#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;
}
- 同步定时器
#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;
}
- 回调函数绑定参数
#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;
}
- 多线程回调函数
#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;
}
三级目录