Boost logo

Boost :

From: Àî»Û°Ô (magazine.lihuiba_at_[hidden])
Date: 2006-07-03 09:14:26

I have used the recent asio library in my small project, the project is successful, but I think debuging the asynchronous code was a nightmare. I am a programmer of 13 years, I have taken part in ACM/ICPC Asia regional contest many times, I take myself as a skillful programmer, but I found it difficult to write asynchronous programme. I believe many other people have the same feeling with me.

Asynchronous program have to pass many state data between different operations, so it is bothering to implement, and it is difficult to debug(due to the loss of many other information), and the most important, it conflicts with normal thinking habit of straight line code.

I have been considering this problem for a period of time, and I find a approach that looks better. The basic idea is to utilize coroutine to simulate a logically synchronized program in the upper layer, and maintain the asynchronous model in the lower layer, using the completion callback to do coroutine switch.

This approach is as efficient as asynchronous model in performance, and is also as simple (to use) as the synchronized approach. With this approach, we can imagine a programe using boost::asio like this:

void foo()
        boost::asio::ipv4::host_resolver resolver(demuxer);
        boost::asio::ipv4::host host;
        resolver.get_host_by_name(host, "");
        boost::asio::ipv4::tcp::endpoint addr(80, host.address(0));
        boost::asio::stream_socket sock(demuxer);
        sync_connect(sock, addr); //using asynchronous IO model in the underlying layer

        static char request[]="GET /\r\n\r\n";
        //using asynchronous IO model in the underlying layer
        size_t sw=sync_write(sock, boost::asio::buffer(request, sizeof(request)-1));

        char buf[10*1024];
        //using asynchronous IO model in the underlying layer
        size_t sr=sync_read(sock, boost::asio::buffer(buf));

I have tried to implement this feature on windows using fiber, but it's not portable at all. So I suggest boost implement a coroutine library, so that this asynchronous model would come true.

Boost list run by bdawes at, gregod at, cpdaniel at, john at