boost::asio::io_service和io_service::work和boost::thread_group配合使用
来源:互联网 发布:烟台正浩网络老总 编辑:程序博客网 时间:2024/06/05 11:49
boost::asio::io_service和io_service::work和boost::thread_group配合使用
-
- #include <iostream>
-
-
- #include <boost/asio.hpp>
-
- #include <boost/thread.hpp>
- long boost_thread_id_to_long(boost::thread::id id)
- {
- std::stringstream ss; ss << id;
- return strtol(std::string(ss.str()).c_str(), (char**)NULL, 16);
- }
-
- #include <boost/date_time/posix_time/posix_time.hpp>
- std::string local_time_to_simple_string()
- {
- boost::posix_time::ptime ptm = boost::posix_time::microsec_clock::local_time();
- return boost::posix_time::to_simple_string(ptm);
- }
-
- #include <boost/format.hpp>
- std::string completion_handler_fun(float f, std::string str)
- {
- str = (boost::format("%|s|, thread_id=%|ld|, f=%|.3f|, str=%|s|")
- % local_time_to_simple_string()
- % boost_thread_id_to_long(boost::this_thread::get_id())
- % f % str.c_str()).str();
- std::cout << str << std::endl;
- return str;
- }
-
- int main1()
- {
- boost::asio::io_service io;
- io.post(boost::bind(completion_handler_fun, 3.141592653f, "111"));
- io.post(boost::bind(completion_handler_fun, 3.141592653f, "222"));
- std::cout << "sleep some seconds before run" << std::endl;
- boost::this_thread::sleep_for(boost::chrono::seconds(3));
- io.run();
- std::cout << "sleep some seconds after run" << std::endl;
- boost::this_thread::sleep_for(boost::chrono::seconds(3));
- return 0;
-
-
- }
-
- int main2()
- {
- boost::asio::io_service io;
-
-
- boost::asio::io_service::work wk(io);
- io.post(boost::bind(completion_handler_fun, 3.141592653f, "111"));
- io.post(boost::bind(completion_handler_fun, 3.141592653f, "222"));
- std::cout << "sleep some seconds before run" << std::endl;
- boost::this_thread::sleep_for(boost::chrono::seconds(3));
- io.run();
-
- std::cout << "io_service::run is end and will exit." << std::endl;
- return 0;
- }
-
- int main3()
- {
- boost::asio::io_service io;
-
- for (int i = 0; i < 100; ++i)
- {
- std::stringstream ss; ss << i;
- io.post(boost::bind(completion_handler_fun, 3.141592653f, std::string(ss.str())));
- }
- boost::thread_group th_gp;
-
- for (int i = 0; i < 3; ++i)
- {
- boost::thread* th_ptr = th_gp.create_thread(boost::bind(&boost::asio::io_service::run, boost::ref(io)));
- std::cout << "thread_group, thread_id=" << boost_thread_id_to_long(th_ptr->get_id()) << std::endl;
- }
-
-
- th_gp.join_all();
- std::cout << "will exit" << std::endl;
- boost::this_thread::sleep_for(boost::chrono::seconds(3));
- return 0;
- }
-
- int main()
- {
- boost::asio::io_service io;
-
- boost::asio::io_service::work wk(io);
- boost::thread_group th_gp;
- for (int i = 0; i < 3; ++i)
- {
- boost::system::error_code error;
- boost::thread* th_ptr = th_gp.create_thread(boost::bind(&boost::asio::io_service::run, boost::ref(io), error));
- std::cout << "thread_group, thread_id=" << boost_thread_id_to_long(th_ptr->get_id()) << std::endl;
- }
- io.post(boost::bind(completion_handler_fun, 3.141592653f, "xxx"));
- th_gp.join_all();
- std::cout << "will exit" << std::endl;
- boost::this_thread::sleep_for(boost::chrono::seconds(3));
- return 0;
- }
0 0