I'm trying to understand C++ coroutines. My expectation in the example below would be, that each asio::post will switch the context/thread to the given threadpool. But something very strange happens. I get the following output:
thread0: 7f0239bfb3c0
thread2: 7f02391f6640 (tp2)
thread3: 7f02387f5640 (tp)
thread4: 7f02387f5640 (tp2)
thread5: 7f02373f3640 (tp)
thread6: 7f02373f3640 (tp2)
thread7: 7f0235ff1640 (tp)
done
So the thread-id of 3 and 4 are the same, but they should run on a different context/threadpool. And 3, 5 and 7 should have the same ID since it's the same context (with just one thread). I assume that I understand some concept wrong. Can you give me a hint?
Thanks
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
boost::asio::thread_pool tp {1};
boost::asio::thread_pool tp2 {10};
boost::asio::awaitable<void> test() {
std::cout << "thread2: " << boost::this_thread::get_id() << std::endl;
co_await boost::asio::post(tp, boost::asio::use_awaitable);
std::cout << "thread3: " << boost::this_thread::get_id() << std::endl;
co_await boost::asio::post(tp2, boost::asio::use_awaitable);
std::cout << "thread4: " << boost::this_thread::get_id() << std::endl;
co_await boost::asio::post(tp, boost::asio::use_awaitable);
std::cout << "thread5: " << boost::this_thread::get_id() << std::endl;
co_await boost::asio::post(tp2, boost::asio::use_awaitable);
std::cout << "thread6: " << boost::this_thread::get_id() << std::endl;
co_await boost::asio::post(tp, boost::asio::use_awaitable);
std::cout << "thread7: " << boost::this_thread::get_id() << std::endl;
}
int main() {
std::cout << "thread0: " << boost::this_thread::get_id() << std::endl;
boost::asio::co_spawn(tp2, &test, [](std::exception_ptr e) { std::cout << "done" << std::endl; });
tp2.join();
tp.join();
}