Very short summarized:

I am implementing a simple TCP server using boost::asio that allows for non-blocking acception of new connnections. Inside the logic of handling the new connection a lot of work will be performed that might take up to several minutes. In particular I am going to start a new process and wait for it to finish while reading stdin, stderr and it's return code. Just image I want to start g++ for every connection, compile several source files and get the build information. While g++ runs in the seperate process I still want to be able to accept new connections.

I am a newcomer to boost::asio and looking for some design advice and input to my current ideas.

Option #1: Using a thread for each connection and detach it

#include <boost/asio.hpp>

#include "tcp_connection.hpp"
#include <thread>
#include <functional>

using boost::asio::ip::tcp;

class tcp_server
  tcp_server(boost::asio::io_context& io_context, unsigned short port_num)
   : m_io_context(io_context),
     m_acceptor(io_context, tcp::endpoint(tcp::v4(), port_num)),
      // create initial connection that will be accepted

    void create_connection()
        // create new connection that will be accepted
        tcp_connection::pointer new_connection = tcp_connection::create(m_io_context);
        // can't mix std::bind with boost::asio::placeholders ...
                                boost::bind(&tcp_server::handle_accept, this,

        // save new connection to be handled next
        m_curr_connection = new_connection;

    void handle_accept(const boost::system::error_code& error)
            // run new connection in own thread 
            std::thread t(std::bind(&tcp_connection::run, m_curr_connection));
            // create next connection that will be accepted
            // detach thread before it goes out of scope 

    boost::asio::io_context& m_io_context;
    tcp::acceptor m_acceptor;
    tcp_connection::pointer m_curr_connection;
    unsigned short m_port_num;

So accepting of the connection is all performed asynchronously in the main-thread using async_accept. For the handling I am creating a worker thread that calls g++ and waits for it to finish. Yet the server can accept new connections in the meanwhile and start new compilations.

The run method of the connection looks similar to this withour error handling

auto prog = boost::process::search_path("g++");
boost::asio::io_context io_context;

std::future<std::string> data;
boost::process::child processCobol(prog, "main.cpp"
                                   boost::process::std_out > boost::process::null,
                                   boost::process::std_err > data,
m_message = data.get();

I am using asynchronous I/O here as well, however, it would also be suffiecent to read the result back synchronously for me at the moment.

Option #2: Using a fork approach

Assume I have a libg++ that I can link straight to the server. I could use a "classic" fork approach for each connection such as shown in here: and put a call to g++_compile() straight after the fork. Still I can accept new connection as I have a separate process for each connection.

Option #3: Use boost::process::spawn and read back outcome via shared memory, e.g using boost::interprocess

Instead of creating a child process I could spawn a new process, detach it and at some point read the outcome back via shared memory. Something tells me that this is not a really good idea D:

Option #4: ?

Is there actually any way to do this without the need of using a helper thread or process for each connection? In the end at some point I need to wait for the result in my main-thread, blocking the acception of new connections. I got my hands on a coroutine a bit, but still lack to understand its details and don't think it will help me either in this scenario.

Thank you very much!

1 Answers

Community On

I did implement #2 ahead of the mentioned example now as well and it seems to work as intended for my needs. Still I would be very interested in some input, especially considering #4.