简体   繁体   中英

boost.asio and queries to the database

I am writing a network application in C++ using boost.asio.

I need to do three things:

  1. make a connection from a client
  2. make a request to the database
  3. return the result to the client.

All operations except 2 are nonblocking. But during the database query all of the application is blocked and non-performing other customers.

How can I be in this situation?

As you haven't provided a detailed description of your case I assume, that you'd like to process connections from clients in many concurent threads. Connection handlers are run in thread, that called io_service::run() .

You can create a pool of threads processing connection handlers (and waiting for finalizing database connection), by running io_service::run() from multiple threads. See Boost.Asio docs .

Unfortunately, there are many external libraries that do not offer asynchronous interfaces. This happens most often when the library attempts to abstract away some important aspect of how it works. In this case, the database interface abstraction is leaking the fact that it blocks on a network request by blocking your thread.

Unless your database provider offers an asynchronous interface (I doubt it, never seen one that did), you will have to live with the fact that this blocks. You basically have to consider it almost equal to a task that runs a CPU-intensive computation with respect to completion.

You will need to either spawn an additional thread to make the request without blocking the asynchronous I/O completion handler or move this work to another process and have that process block on the database queries instead. This is common design in web servers, where the gateway just forwards I/O to an application server which then does its work in a synchronous fashion without blocking the gateway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM