Boost logo

Boost Users :

Subject: Re: [Boost-users] async_send handler problem
From: Alexander Carôt (Alexander_Carot_at_[hidden])
Date: 2013-12-17 10:54:40

>>Yes, I truly understand this but for some weired reason this doesn't work
>> at all. In my case I launch the run function in the constructor of the
>> main class right after the sender class had been instantiated:
>> SJ::SJ(){
>> dFC->mySender = new sender(this);
>> boost::thread t =
>> boost::thread(boost::bind(&boost::asio::io_service::run,
>> &dFC->io_service));
>> t.join();
>> // OR dFC->;
>> }
>> In there anything wrong with this ?
>It seems that you call io_serivce::run() before any async operation
>was issued. In such a case, run() just exits immediately, as I
>explained in one of my previous comments. In order to avoid such a
>associate io_service::work object with this io_service:

Allright - this helps but still issues (see below).

>In general, it's highly recommended to read the Asio documentation -
>all these issues are described there.

Yes, you can assume this but it seems the case is a little more complex
than it is the case with the default use cases:

This simple example works fine:

boost::asio::io_service io_service;

int i = 0;

void handle_send(const boost::system::error_code& error, std::size_t
  std::cout << "async_send_to return " << error << ": " <<
bytes_transferred << " transmitted " << i << std::endl;

int main(int argc, char *argv[]){

  boost::asio::io_service::work work(io_service);
  boost::thread t = boost::thread
(boost::bind(&boost::asio::io_service::run, &io_service));

  boost::asio::ip::udp::socket socket(io_service);
  boost::asio::ip::udp::endpoint remote_endpoint;;

  remote_endpoint = boost::asio::ip::udp::endpoint(

  std::cout << "Send to " << remote_endpoint << std::endl;

  while (1)
  socket.async_send_to(boost::asio::buffer("message", 7), remote_endpoint,

return 0;

but with respect to my app this does not work:

I am specifying the io_service thread in my constructor.

  boost::asio::io_service::work work(dFC->io_service);
  boost::thread myThread(boost::bind(&boost::asio::io_service::run,

I have a handle in my custom SJ class:

void SJ::handleSend(const boost::system::error_code& error, std::size_t
  cout << "SENT FROM HANDLER" << endl;

I have a send function:

void SJ::sendIt(){
  cout << "Send Funktion" << endl;
  dFC->s->async_send_to(boost::asio::buffer("message", 7),
  boost::bind(&handleSender, boost::asio::placeholders::error,

The send function is called from an *non-class-member* callback thread

static int audioCallback( void *data ){
  SJ::callbackdata *my;
  my = (SJ::callbackdata *) data;


Not that everything works fine when I call the send function from within
my SJ-class but when I call the send function from the external callback
thread the handler is not triggering anymore.

Does this help to clarify what (or where) my problem is ?

Thanks again


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at