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)));
}

 

你可能感兴趣的:(service)