Boost.Asio和Boost的联系和区别

 我来答
匿名用户
2016-05-21
展开全部
namespace
{
// strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行.
// io_service不能保证线程安全
boost::asio::io_service m_service;
boost::asio::strand m_ www.hbbz08.com strand(m_service);
boost::mutex m_mutex;

void print(int id)
{
// boost::mutex::scoped_lock lock(m_mutex);
static int count = 0;
PRINT_DEBUG("id: " << boost::lexical_cast<std::string>(id));
PRINT_DEBUG("count: " << boost::lexical_cast<std::string>(++count));
}

void ioRun1()
{
while(true)
{
m_service.run();
}
}

void ioRun2()
{
while(true)
{
m_service.run();
}
}

void strand_print1()
{
// PRINT_DEBUG("Enter print1");
m_strand.dispatch(boost::bind(print, 1));
// PRINT_DEBUG("Exit print1");
}

void strand_print2()
{
// PRINT_DEBUG("Enter print2");
m_strand.post(boost::bind(print, 2));
// PRINT_DEBUG("Exit print2");
}

void strand_print3()
{
// PRINT_DEBUG("Enter print3");
m_strand.post(boost::bind(print, 3));
// PRINT_DEBUG("Exit print3");
}

void strand_print4()
{
// PRINT_DEBUG("Enter print4");
m_strand.post(boost::bind(print, 4));
// PRINT_DEBUG("Exit print4");
}

// 将上面的m_strand换成m_service后,
void service_print1()
{
// PRINT_DEBUG("Enter print1");
m_service.dispatch(boost::bind(print, 1));
// PRINT_DEBUG("Exit print1");
}

void service_print2()
{
// PRINT_DEBUG("Enter print2");
m_service.post(boost::bind(print, 2));
// PRINT_DEBUG("Exit print2");
}

void service_print3()
{
// PRINT_DEBUG("Enter print3");
m_service.post(boost::bind(print, 3));
// PRINT_DEBUG("Exit print3");
}

void service_print4()
{
// PRINT_DEBUG("Enter print4");
m_service.post(boost::bind(print, 4));
// PRINT_DEBUG("Exit print4");
}
}

void test_strand()
{
boost::thread ios1(ioRun1);
boost::thread ios2(ioRun2);

boost::thread t1(strand_print1);
boost::thread t2(strand_print2);
boost::thread t3(strand_print3);
boost::thread t4(strand_print4);

t1.join();
t2.join();
t3.join();
t4.join();

m_server.run();
}

void test_service()
{
boost::thread ios1(ioRun1);
boost::thread ios2(ioRun2);

boost::thread t1(service_print1);
boost::thread t2(service_print2);
boost::thread t3(service_print3);
boost::thread t4(service_print4);

t1.join();
t2.join();
t3.join();
t4.join();

m_service.run();
}
test_strand的执行结果:
[cpp] view plain copy print?
2013-01-05 17:25:34 626 [8228] DEBUG - id: 4
2013-01-05 17:25:34 631 [8228] DEBUG - count: 1
2013-01-05 17:25:34 634 [5692] DEBUG - id: 1
2013-01-05 17:25:34 637 [5692] DEBUG - count: 2
2013-01-05 17:25:34 640 [5692] DEBUG - id: 2
2013-01-05 17:25:34 642 [5692] DEBUG - count: 3
2013-01-05 17:25:34 646 [5692] DEBUG - id: 3
2013-01-05 17:25:34 649 [5692] DEBUG - count: 4
test_ioserivice的执行结果:
[cpp] view plain copy print?
2013-01-05 17:26:28 071 [3236] DEBUG - id: 1
2013-01-05 17:26:28 071 [5768] DEBUG - id: 2
2013-01-05 17:26:28 071 [5108] DEBUG - id: 3
2013-01-05 17:26:28 076 [3236] DEBUG - cou
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式