Multithreaded web server based on C + + 11

Time:2020-1-17

I wrote a lightweight web server by referring to Linux multithreading server programming and imitating the Muduo used in the book.

Source code

Github. There are more detailed comments in the code, which can be used as auxiliary materials to see Linux multithreading server programming.

Completed

  • Complete the basic TCP (passive connection) library;
  • Simple HTTP server with access to homepage HTML and website favicon icon.

technology

  • Master slave reactor mode:

    • The main reactor is responsible for monitoring the connection. When there is a new connection, accept to the new socket, use the round robin method to select the slave reactor and send the socket to the slave reactor;
    • The time descriptor (timerfd for timed tasks), event descriptor (eventfd for waking IO threads), and socket file descriptor sent from reactor are managed.
  • multiple reactors + thread pool (one loop per thread + threadpool);
  • EventLoop: using epoll level triggered IO multiplexing technology, non blocking IO;
  • Thread pool:

    • Using multithreading can give full play to the advantage of multi-core;
    • Thread pool can avoid the overhead of frequent creation and destruction of threads.
  • Simple log system;
  • Use the raii mechanism such as intelligent pointer to reduce the possibility of memory leakage;

Concurrent model

The concurrency model is multiple reactors + thread pool (one loop per thread + ThreadPool); + non blocking io. New connections are dispatched using round robin policy.

Multithreaded web server based on C + + 11

Pressure measurement

Pressure test results.

Reference resources

  • Linux multithreading server programming
  • muduo
  • Mr. Chen Shuo’s blog
  • Linyacool’s webserver
  • uv-cpp
  • Open source HTTP parser
  • HTTP request and response formats
  • Write a concurrent HTTP server (with code)
  • What is the recommended open source project for improving the network programming ability of C / C + +? -Answer to variation – Zhihu (Master / worker, Proctor and other IO models)
  • Two design and implementation of reactor event driven: object oriented vs functional programming
  • Summary of key problems in network programming
  • Muduo source code analysis – cyhone’s article – Zhihu
  • Muduo source code analysis