I tried the example from the boost docs:
#include <boost/thread/thread.hpp>
#include <iostream>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
int count = 0;
void my_task() {
count++;
const auto my_count = count;
std::cout << "Task " << my_count << ") BEGIN: Thread-ID:" << boost::this_thread::get_id << std::endl;
boost::this_thread::sleep(1000000000);
std::cout << "Task " << my_count << ") END: Thread-ID:" << boost::this_thread::get_id << std::endl;
}
int main(int argc, char** argv) {
std::cout << "Main-thread-ID: " << boost::this_thread::get_id << std::endl;
// Launch the pool with two threads.
boost::asio::thread_pool pool(2);
// Submit a function to the pool.
boost::asio::post(pool, my_task);
boost::asio::post(pool, my_task);
boost::asio::post(pool, my_task);
boost::asio::post(pool, my_task);
boost::asio::post(pool, my_task);
// Wait for all tasks in the pool to complete.
pool.join();
return 0;
}
The output is:
Main-thread-ID: 00CC96C6
Task 1) BEGIN: Thread-ID:00CC96C6
Task 2) BEGIN: Thread-ID:00CC96C6
Task 1) END: Thread-ID:00CC96C6
Task 3) BEGIN: Thread-ID:00CC96C6
Task 2) END: Thread-ID:00CC96C6
Task 4) BEGIN: Thread-ID:00CC96C6
Task 4) END: Thread-ID:00CC96C6
Task 3) END: Thread-ID:00CC96C6
Task 5) BEGIN: Thread-ID:00CC96C6
Task 5) END: Thread-ID:00CC96C6
Why does the same thread ID show up the whole time? Have I done something wrong? It still does execute my tasks parallel. So, I'm not too worried.
I've seen the same behavior when I tried a similiar example with boost::thread_group
.
Thanks Kaldrr! I changed all occurances of boost::this_thread::get_id
to boost::this_thread::get_id()
which led to the expected output of different Thread IDs:
Main-thread-ID: 5a78
Task 1) BEGIN: Thread-ID:77d0
Task 1) BEGIN: Thread-ID:5f34
Task 1) END: Thread-ID:5f34
Task 1) END: Thread-ID:77d0
Task 2) BEGIN: Thread-ID:5f34
Task 3) BEGIN: Thread-ID:77d0
Task Task 2) END: Thread-ID:5f34
3) END: Thread-ID:77d0
Task 4) BEGIN: Thread-ID:77d0
Task 4) END: Thread-ID:77d0