anarthal / servertech-chat

Chat app using Boost and C++
https://anarthal.github.io/servertech-chat/
Boost Software License 1.0
9 stars 3 forks source link

Multithreading #25

Open anarthal opened 10 months ago

anarthal commented 10 months ago

We currently run a single-threaded io_context. Consider how we can make it multi-threaded (one context per thread vs. multiple threads and one context).

mzimbres commented 10 months ago

My suggestion here is to use share-nothing io_contexts, the exact number will depend on the number of CPU cores available, so it would be good to make this a command like parameter. To be able to have one listener per io_context listening on the same port, you have the set SO_REUSEPORT like this

   int one = 1;
   auto const ret =
      setsockopt( acceptor_.native_handle()
                , SOL_SOCKET
                , SO_REUSEPORT
                , &one, sizeof(one));

   if (ret == -1) {
      log::write( log::level::err
                , "Unable to set socket option SO_REUSEPORT: {0}"
                , strerror(errno));
   }