| 114 | } |
| 115 | |
| 116 | void strand_test() |
| 117 | { |
| 118 | io_context ioc; |
| 119 | io_context::strand s(ioc); |
| 120 | int count = 0; |
| 121 | |
| 122 | post(ioc, bindns::bind(increment_without_lock, &s, &count)); |
| 123 | |
| 124 | // No handlers can be called until run() is called. |
| 125 | BOOST_ASIO_CHECK(count == 0); |
| 126 | |
| 127 | ioc.run(); |
| 128 | |
| 129 | // The run() call will not return until all work has finished. |
| 130 | BOOST_ASIO_CHECK(count == 1); |
| 131 | |
| 132 | count = 0; |
| 133 | ioc.restart(); |
| 134 | post(s, bindns::bind(increment_with_lock, &s, &count)); |
| 135 | |
| 136 | // No handlers can be called until run() is called. |
| 137 | BOOST_ASIO_CHECK(count == 0); |
| 138 | |
| 139 | ioc.run(); |
| 140 | |
| 141 | // The run() call will not return until all work has finished. |
| 142 | BOOST_ASIO_CHECK(count == 1); |
| 143 | |
| 144 | count = 0; |
| 145 | ioc.restart(); |
| 146 | post(ioc, bindns::bind(start_sleep_increments, &ioc, &s, &count)); |
| 147 | boost::asio::detail::thread thread1(bindns::bind(io_context_run, &ioc)); |
| 148 | boost::asio::detail::thread thread2(bindns::bind(io_context_run, &ioc)); |
| 149 | |
| 150 | // Check all events run one after another even though there are two threads. |
| 151 | timer timer1(ioc, chronons::seconds(3)); |
| 152 | timer1.wait(); |
| 153 | BOOST_ASIO_CHECK(count == 0); |
| 154 | timer1.expires_at(timer1.expiry() + chronons::seconds(2)); |
| 155 | timer1.wait(); |
| 156 | BOOST_ASIO_CHECK(count == 1); |
| 157 | timer1.expires_at(timer1.expiry() + chronons::seconds(2)); |
| 158 | timer1.wait(); |
| 159 | BOOST_ASIO_CHECK(count == 2); |
| 160 | |
| 161 | thread1.join(); |
| 162 | thread2.join(); |
| 163 | |
| 164 | // The run() calls will not return until all work has finished. |
| 165 | BOOST_ASIO_CHECK(count == 3); |
| 166 | |
| 167 | count = 0; |
| 168 | int exception_count = 0; |
| 169 | ioc.restart(); |
| 170 | post(s, throw_exception); |
| 171 | post(s, bindns::bind(increment, &count)); |
| 172 | post(s, bindns::bind(increment, &count)); |
| 173 | post(s, throw_exception); |