Ticket #9439: test_spawn.cpp

File test_spawn.cpp, 1.6 KB (added by 348944179@…, 8 years ago)

success tested 500000 coroutines using boost 1.56.0

Line 
1
2// success tested on windows7 x64 vc11 and opensuse13.1 gcc4.8.1 amd64, using boost 1.56.0
3// failed on boost 1.55.0
4// 348944179@qq.com
5// nousxiong
6
7#define BOOST_ASIO_DISABLE_STD_CHRONO
8
9#include <boost/asio.hpp>
10#include <boost/asio/spawn.hpp>
11#include <boost/asio/system_timer.hpp>
12#include <boost/thread.hpp>
13#include <boost/chrono.hpp>
14#include <boost/bind.hpp>
15#include <boost/ref.hpp>
16#include <deque>
17#include <iostream>
18
19void echo(boost::asio::yield_context yld, boost::asio::io_service& ios)
20{
21 boost::asio::system_timer tmr(ios);
22 tmr.expires_from_now(boost::chrono::seconds(30));
23 tmr.async_wait(yld);
24}
25
26int main()
27{
28 try
29 {
30 std::cout << "begin\n";
31 std::size_t thread_num = 10;
32 boost::asio::io_service ios;
33 std::deque<boost::asio::strand> strand_list;
34 for (std::size_t i=0; i<thread_num; ++i)
35 {
36 // note: gcc must set -std=c++11
37 strand_list.emplace_back(boost::ref(ios));
38 }
39 boost::thread_group thrs;
40
41 for (std::size_t i=0; i<500000; ++i)
42 {
43 boost::asio::spawn(
44 strand_list[i % thread_num],
45 boost::bind(
46 &echo, _1, boost::ref(ios)
47 ),
48 boost::coroutines::attributes(
49 boost::coroutines::stack_traits::minimum_size()
50 )
51 );
52 }
53
54 for (std::size_t i=0; i<thread_num; ++i)
55 {
56 thrs.create_thread(boost::bind(&boost::asio::io_service::run, &ios));
57 }
58 thrs.join_all();
59
60 std::cout << "end\n";
61 }
62 catch (std::exception& ex)
63 {
64 std::cerr << ex.what() << std::endl;
65 }
66 return 0;
67}
68