- #include <iostream>
- #include <vector>
- #include <boost/shared_ptr.hpp>
- #include <boost/bind.hpp>
- #include <boost/asio/io_service.hpp>
- //#include <io_service_pool.h>
- class worker
- {
- public:
- worker (unsigned int id)
- : id_ (id){}
- virtual void process () = 0;
- protected:
- unsigned int id_;
- };
- #define DECLARE_CLASS(c)\\
- class c : public worker\\
- {\\
- public:\\
- c(unsigned int id);\\
- virtual void process ();\\
- }
- #define IMPLEMENT_CLASS(c)\\
- c::c(unsigned int id)\\
- : worker (id){}\\
- void c::process ()\\
- {\\
- std::cout << #c << id_ << " process." << std::endl;\\
- }
- DECLARE_CLASS (receive_worker);
- IMPLEMENT_CLASS (receive_worker)
- DECLARE_CLASS (send_worker);
- IMPLEMENT_CLASS (send_worker)
- DECLARE_CLASS (operation_worker);
- IMPLEMENT_CLASS (operation_worker)
- int main (void)
- {
- std::vector <boost::shared_ptr <worker> > wv;
- wv.push_back (boost::shared_ptr <worker> (new receive_worker (0)));
- wv.push_back (boost::shared_ptr <worker> (new receive_worker (1)));
- wv.push_back (boost::shared_ptr <worker> (new receive_worker (2)));
- wv.push_back (boost::shared_ptr <worker> (new operation_worker (0)));
- wv.push_back (boost::shared_ptr <worker> (new operation_worker (1)));
- wv.push_back (boost::shared_ptr <worker> (new operation_worker (2)));
- wv.push_back (boost::shared_ptr <worker> (new send_worker (0)));
- wv.push_back (boost::shared_ptr <worker> (new send_worker (1)));
- wv.push_back (boost::shared_ptr <worker> (new send_worker (2)));
- boost::asio::io_service io_service;
- for (unsigned int i = 0; i < wv.size (); ++i)
- {
- io_service.post (boost::bind (&worker::process, wv[i]));
- }
- io_service.run ();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/230620149853.html
来源: http://www.codesnippet.cn/detail/230620149853.html