Thread-ID 在我的 boost::asio::thread_pool 中始终相同
Thread-ID is always the same in my boost::asio::thread_pool
我尝试了 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;
}
输出为:
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
为什么一直出现同一个线程ID?我做错了什么吗?它仍然并行执行我的任务。所以,我不太担心。
当我尝试 similiar example 和 boost::thread_group
时,我看到了相同的行为。
谢谢!我将所有出现的 boost::this_thread::get_id
更改为 boost::this_thread::get_id()
,这导致了不同线程 ID 的预期输出:
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
我尝试了 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;
}
输出为:
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
为什么一直出现同一个线程ID?我做错了什么吗?它仍然并行执行我的任务。所以,我不太担心。
当我尝试 similiar example 和 boost::thread_group
时,我看到了相同的行为。
谢谢boost::this_thread::get_id
更改为 boost::this_thread::get_id()
,这导致了不同线程 ID 的预期输出:
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