我目前正在学习如何使用和实现boost :: asio。我一直在" https://www.gamedev.net/blogs/entry/2249317-a-guide-to-getting-started-with-boostasio/"
上关注gamedev.net上的教程在本教程中,将显示以下代码:
#include <iostream>
#include "/usr/include/boost/asio.hpp"
#include "/usr/include/boost/thread.hpp"
#include "/usr/include/boost/lexical_cast.hpp"
boost::mutex global_stream_lock;
void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl;
global_stream_lock.unlock();
io_service->run();
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id() << "] Thread Finish" << std::endl;
global_stream_lock.unlock();
}
void PrintNum( int x )
{
std::cout << "[" << boost::this_thread::get_id() << "] x: " << x << std::endl;
}
int main( int argc, char * argv[] )
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work( *io_service )
);
boost::asio::io_service::strand strand (*io_service);
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] The program will exit when all work has finished." << std::endl;
global_stream_lock.unlock();
boost::thread_group worker_threads;
for( int x = 0; x < 2; ++x )
{
worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
}
boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
strand.post( boost::bind( &PrintNum, 1 ) );
strand.post( boost::bind( &PrintNum, 2 ) );
strand.post( boost::bind( &PrintNum, 3 ) );
strand.post( boost::bind( &PrintNum, 4 ) );
strand.post( boost::bind( &PrintNum, 5 ) );
work.reset();
worker_threads.join_all();
return 0;
}
执行上述代码时的输出:
[7fef17715000] The program will exit when all work has finished.
[7fef15897700] Thread Start
[7fef16098700] Thread Start
[7fef15897700] x: 1
[7fef15897700] x: 2
[7fef15897700] x: 3
[7fef15897700] x: 4
[7fef15897700] x: 5
[7fef15897700] Thread Finish
[7fef16098700] Thread Finish
我的问题是上面的代码只使用thread_id 7fef15897700并且从未使用过7fef16098700。在示例教程中,输出显示如下:
[00154F88] The program will exit when all work has finished.
[001532B0] Thread Start
[00154FB0] Thread Start
[001532B0] x: 1
[00154FB0] x: 2
[001532B0] x: 3
[00154FB0] x: 4
[001532B0] x: 5
[00154FB0] Thread Finish
[001532B0] Thread Finish
我正在使用libboost 1.66,它的构建方式如下:
./bootstrap.sh --prefix=/usr
./b2 install threading=multi
我正在运行debian,如下所示:
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
&#34; lscpu | grep -i cpu&#34;输出:
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 4
On-line CPU(s) list: 0-3
CPU family: 6
Model name: Intel(R) Core(TM) i5 CPU 650 @ 3.20GHz
CPU MHz: 1197.000
CPU max MHz: 3193.0000
CPU min MHz: 1197.0000
NUMA node0 CPU(s): 0-3
如何让代码使用thread_group中的所有可用线程?
感谢您提供任何帮助或意见。