Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost: is it safe to use multiple recursion in async calls?

I'm pretty new in asio framework, so please be kind. I've investigated several boost asio example and found that people use async call like this:

void read()
{
    async_read(socket_, boost::asio::buffer(&user_[0], user_.size()),
       boost::bind(&Connection::handle_user_read, this,
       placeholders::error, placeholders::bytes_transferred));
}
void handle_user_read(...)
{
    ...
    read();
    ...
}

I think this code is not safe because it uses multiple recursion. So it cant be used when a lot of read operations are performed because of call stack overflow. I'm not 100% sure in this and unable to find any similar thoughts from other people.

Could anyone please explain this in details?

like image 673
Vladimir Tsyshnatiy Avatar asked Nov 18 '15 09:11

Vladimir Tsyshnatiy


1 Answers

I suppose the read() function just adds new async read request to the I/O queue and exits immediately, so there's no recursion in this code.

I mean, the read() doesn't call Connection::handle_user_read directly. It just store the function pointer inside the IO queue. This function is going to be invoked asynchronously by external code when some new chunk of data will be available.

like image 91
Minor Threat Avatar answered Nov 14 '22 22:11

Minor Threat