boost中bind、thread、io_services测试

// BoostTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#define BOOST_MEM_FN_ENABLE_STDCALL
#define BOOST_MEM_FN_ENABLE_FASTCALL

#include <boost/cstdint.hpp>
#include <boost/bind/bind.hpp>

#include <boost/asio/io_service.hpp>
#include <boost/asio/strand.hpp>

#include <boost/thread/thread.hpp>
#include <boost/thread/detail/thread_group.hpp>

#include <boost/shared_ptr.hpp>

#define STDCALL _stdcall

class IPrint{
public:
virtual void STDCALL print1() = 0;
};

class CPrint : public IPrint
{
public:
CPrint(){}
public:
virtual void STDCALL print1()
{
printf("%s called.\n",__FUNCTION__);
}

void STDCALL print2()
{
printf("%s called.\n",__FUNCTION__);
}

void print_number(int value){
printf("print_number(%d) called.\n", value);
}
};

void OtherTask(boost::asio::io_service & service, boost::asio::io_service::strand &strand);

int _tmain(int argc, _TCHAR* argv[])
{

CPrint printer;
IPrint *pPrinter = &printer;

boost::asio::io_service service;
boost::asio::io_service::work work(service);
boost::asio::io_service::strand strand(service);
boost::thread_group thread_group;

//normal stdcall
thread_group.create_thread(boost::bind(&CPrint::print2, &printer));

//virtual stdcall
thread_group.create_thread(boost::bind(&CPrint::print1, &printer));

//parent class call virtual stdcall fuction
thread_group.create_thread(boost::bind(&IPrint::print1, pPrinter));

//call function with argument
thread_group.create_thread(boost::bind(&CPrint::print_number, &printer, 12));

//normal stdcall
thread_group.create_thread(strand.wrap(boost::bind(&CPrint::print2, &printer)));

//virtual stdcall
thread_group.create_thread(strand.wrap(boost::bind(&CPrint::print1, &printer)));

//parent class call virtual stdcall fuction
thread_group.create_thread(strand.wrap(boost::bind(&IPrint::print1, pPrinter)));

//call function with argument
thread_group.create_thread(strand.wrap(boost::bind(&CPrint::print_number, &printer, 12)));

//boost::thread::sleep(boost::system_time(1000));

service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 0)));
service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 1)));
service.dispatch(strand.wrap(boost::bind(&CPrint::print_number, &printer, 2)));
service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 4)));
service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 5)));

boost::thread::sleep(boost::get_system_time()+boost::posix_time::milliseconds(20));

//boost::this_thread::sleep(boost::posix_time::milliseconds(20));

boost::thread *service_thread = thread_group.create_thread(boost::bind(&boost::asio::io_service::run, &service));

thread_group.create_thread(strand.wrap(boost::bind(&OtherTask, boost::bind::ref(service), boost::bind::ref(strand))));

thread_group.join_all();

//service.reset();
//service.run();

return 0;
}

void OtherTask(boost::asio::io_service & service, boost::asio::io_service::strand &strand)
{
service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 6)));
service.dispatch(strand.wrap(boost::bind(&CPrint::print_number, &printer, 7)));
service.post(strand.wrap(boost::bind(&CPrint::print_number, &printer, 8)));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值