c++cboostfiber

Does Boost.Fiber automatically yield on network requests, such as a database call over the network?


Does Boost.Fiber automatically yield on network requests (if I understand correctly they yield the CPU during I/O), such as a database call over the network? I want to use it for setting up blocking database calls where I am inserting a lot of information and have a lot of small records that need to be inserted when they are received. My other choice would be libdill/libmill for the microthreading.

The general idea would be:

  1. receive data
  2. create fiber and pass it the data
  3. fiber gets a database connection and sends the INSERT query (libpqxx with postgres)
  4. other fibers run and can access the database while the database query is in flight
  5. query returns from database, do error handling and process response

Do fibers work this way as I am envisioning it?

This is the simplified base structure for the code using RapidJSON and boost.fiber, but I'm getting the messages from the network (websocket):

inline
void doDatabaseCall( Document &msg ) {
     //Get connection & do query with libpqxx
}

Document msg;
msg.Parse( "{\"myVar\": \"test\"}" );
Document msg2;
msg2.Parse( "{\"myVar\": \"test2\"}" );
boost::fibers::fiber f1( doDatabaseCall, msg);
boost::fibers::fiber f2( doDatabaseCall, msg2);
f1.join();
f2.join();

Solution

  • Does Boost.Fiber automatically yield on network requests (if I understand correctly they yield the CPU during I/O), such as a database call over the network?

    No they do not.

    What you seem to be looking for would be Boost Coroutines, combined with Boost Asio for the asynchronous IO. They will yield.

    There are libraries for database operations that exploit Asio for asynchronous execution (e.g. Amy) or WebSockets (e.g. Boost Beast)