title: How do I make the coroutine always work in the same thread?
body: <p>I want to accept connection in thread-0, then post this socket to one of other threads (1 - 31) <strong>for load-balancing</strong>, and I want then all other operations with this socket will be in the same thread, by using coroutine - to <strong>avoid inter-thread context switchin</strong>g.</p>
  
  <ul>
  <li><p>I want to perform async-callback on different threads for load balancing by using <code>io_service</code>+<code>work</code>+<code>vector&lt;boost::thread&gt;</code>.</p></li>
  <li><p>But at the same time I want to perform all other operations with this socket by using coroutine only within the same thread - without load balancing and with fast context switching.</p></li>
  </ul>
  
  <p>If I use boost.coroutine1 by using <code>boost::asio::spawn()</code> then can the coroutine be performed alternately on different threads?</p>
  
  <p>And if it can, then how do I make the call-back of coroutine always execute in the same thread from which the asynchronous operation was called, even if to <code>io_service</code> are binded many threads (thread-pool)?</p>
  
  <pre><code>    for (size_t i = 0; i &lt; thread_num_executors; ++i)
          thr_grp_executors.emplace_back(
              boost::bind(&amp;boost::asio::io_service::run, &amp;io_service));
  </code></pre>
  
  <p>As known, switching between the coroutines is very fast, it takes about <strong>10-12 ns</strong> on x86_64: <a href="http://www.boost.org/doc/libs/1_64_0/libs/coroutine/doc/html/coroutine/performance.html" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_64_0/libs/coroutine/doc/html/coroutine/performance.html</a></p>
  
  <p>But this is only true if switching of coroutines occurs within a single thread. Because thread-switching took <strong>more than 100 ns</strong>.</p>
  
  <p>Then how do I make the coroutine always work in the same thread?</p>