Gavin,
Since I will now be using one io_service object, how can I stop the threads from blocking? When I had an io_service per object, I could just call io_service.stop() and I could successfully call a join() on the thread. Since I am now passing a reference to an io_service object, it seems as though the thread will not join().
My PingX constructor is as follows:
public:
PingX( boost::asio::io_service & io_, const char* destination, std::vector<std::string> & ping_results, int index=0) : ping_results_(ping_results),
ip_addr(destination), strand_(io_), resolver_(io_), socket_(io_, icmp::v4()),
timer_(io_), io_service_(io_), sequence_number_(0), num_replies_(0)
{
socket_.non_blocking(true);
boost::asio::socket_base::reuse_address option(true);
socket_.set_option(option);
_index_ = index;
icmp::resolver::query query(icmp::v4(), ip_addr, "");
destination_ = *resolver_.resolve(query);
strand_.post( boost::bind( &GenericMultiPing::start_send, this) );
strand_.post( boost::bind( &GenericMultiPing::start_receive, this) );
}
The async functions in start_send and start_receive are strand.wrap() 'd. I call timer.cancel() as well as socket.close() and it seems I can't get it to unblock. Any ideas?
Main thread would be put together something like:
boost::asio::io_service io_;
boost::asio::io_service::work work_(io_);
int max_threads = 4;
int t_count = 0;
for (..i..)
{
//buffer etc etc
p.push_back( new XPing ( io_, buffer.data(), boost::ref(ping_results), i) );
threads.push_back( new boost::thread ( boost::bind(&boost::asio::io_service::run, boost::ref(io_) ) ) );
t_count++;
if(t_count >= max_threads) //join threads after max threads
{
for(...j...)
{
threads[j]->join();
}
t_count = 0;
}
}
//cleanup threads, etc
So, I have two vectors. "p" is a vector of XPing pointers and "threads" is where I store the threads. I also pass a ref to a separate vector which is just a vector of ping results.
Any ideas?