boost strand任务排序

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;

void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Start" << std::endl;
global_stream_lock.unlock();

io_service->run();

global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Finish" << std::endl;
global_stream_lock.unlock();
}

void PrintNum(int x)
{
std::cout << "[" << boost::this_thread::get_id()
<< "] x: " << x << std::endl;
}

int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::asio::io_service::strand strand(*io_service);

global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] The program will exit when all work has finished." << std::endl;
global_stream_lock.unlock();

boost::thread_group worker_threads;
for (int x = 0; x < 2; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
}

boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
/*
strand.post( boost::bind( &PrintNum, 1 ) );
strand.post( boost::bind( &PrintNum, 2 ) );
strand.post( boost::bind( &PrintNum, 3 ) );
strand.post( boost::bind( &PrintNum, 4 ) );
strand.post( boost::bind( &PrintNum, 5 ) );
*/

io_service->post(boost::bind(&PrintNum, 1));
io_service->post(boost::bind(&PrintNum, 2));
io_service->post(boost::bind(&PrintNum, 3));
io_service->post(boost::bind(&PrintNum, 4));
io_service->post(boost::bind(&PrintNum, 5));
work.reset();

worker_threads.join_all();

return 0;
}

输出是任意的:
[00154F88] The program will exit when all work has finished.
[001532B0] Thread Start
[00154FB0] Thread Start
[[001532B000154FB0] x: ] x: 21
[[001532B000154FB0] x: ] x: 34
[001532B0] x: 5
[00154FB0] Thread Finish
[001532B0] Thread Finish
Press any key to continue . . .

#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include<boost/asio.hpp>
#include<iostream>

boost::mutex global_stream_lock;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl;
    global_stream_lock.unlock();
    
    io_service->run();
    
    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
    << "] Thread Finish" << std::endl;
    global_stream_lock.unlock();
}

void PrintNum( int x )
{
    std::cout << "[" << boost::this_thread::get_id()
    << "] x: " << x << std::endl;
}

int main( int argc, char * argv[] )
{
    boost::shared_ptr< boost::asio::io_service > io_service(
                                                            new boost::asio::io_service
                                                            );
    boost::shared_ptr< boost::asio::io_service::work > work(
                                                            new boost::asio::io_service::work( *io_service )
                                                            );
    boost::asio::io_service::strand strand( *io_service );
    
    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
    << "] The program will exit when all work has finished." << std::endl;
    global_stream_lock.unlock();
    
    boost::thread_group worker_threads;
    for( int x = 0; x < 4; ++x )
    {
        worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
    }
    
    boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 1 ) ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 2 ) ) );
    
    boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 3 ) ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 4 ) ) );
    
    boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 5 ) ) );
    io_service->post( strand.wrap( boost::bind( &PrintNum, 6 ) ) );
    
    work.reset();
    
    worker_threads.join_all();
    
    return 0;
}

[0x100469380] The program will exit when all work has finished.
[0x7000012ab000] Thread Start
[0x70000132e000] Thread Start
[0x7000013b1000] Thread Start
[0x700001434000] Thread Start
[0x7000012ab000] x: 1
[0x7000012ab000] x: 2
[0x70000132e000] x: 3
[0x70000132e000] x: 4
[0x700001434000] x: 5
[0x700001434000] x: 6
[0x7000013b1000] Thread Finish
[0x7000012ab000] Thread Finish
[0x70000132e000] Thread Finish
[0x700001434000] Thread Finish
Program ended with exit code: 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值