Applications with the usual multi-threaded server programming model

This article is excerpted Chen Shuo teacher linux multi-threaded server-side programming

1. The common single-threaded server programming model

non-blocking + IO multiplexing model, i.e. Reactor Model.

Reactor model isEvent-driven model, Concurrent with one or more input sources, similarly to the producer and consumer model, one or more producers in the event into a Queue, and one or more active consumer to handle events from the Poll queue.
Reactor-based event callback function must be non-blocking, otherwise it will affect the normal operation of other connections.

For example:
If an event occurs, socket readable, but the protocol stack checking into this new section inspection and error, then discarded this section, this time calling read no data to read, if it is blocked, it will clog the read .

2. Multi-Threaded Server common programming model

one loop per thread + thread pool this mode requires a quality-based Reactor libraries to support.

  • one loop per thread
    program where each thread has a IO Event Loop (non-blocking + IO multiplexing) or call Reactor

  • Thread pool
    is a multithreaded processing form, the process will add tasks to the queue, and then create a thread to start the task. Then start these tasks automatically when a thread is created. Thread pool threads are background threads. Each thread uses the default stack size to default priority run, and in multi-threaded unit.

3. The application of multi-threaded server applications

select Running multiple single-threaded processes This model

  • Must be single-threaded applications
    might use fork program, only a single thread can fork

Advantages: avoid excessive computing resources to snatch the system
Cons: can not set priorities

  • Suitable for multi-threaded scenarios
  • Shared data can be modified
  • Provide heterogeneous services (Priority difference)

4. Thread Category

A multi-threaded server program that the thread can be roughly divided into three categories:

  • IO thread
    main loop of these threads is IO multiplexing, blocking waiting on select / poll / epoll_wait system calls
  • Compute threads
  • Third-party libraries used thread

Example 5. Explanation

  • Linux how many threads can be started simultaneously?
    And process space with the default thread stack size.
  • Multithreading can improve concurrency it?
    Multithreading number of concurrent connections can not be improved;
    (1) If "a connecting thread a", the number of concurrent connections to associated threads simultaneously started above, it is limited.
    (2) if "one loop per thread", a single event loop processing ten thousand long connections is not uncommon. (As long as the arrival of the main thread listens for connections, after establishing a connection to the callback function assigned to handle)
  • Multithreading can improve throughput it?
    For compute-intensive service, multi-threading can not improve throughput.
  • How to make multithreaded programs IO and computational overlap, reduce latency?
    The basic idea is to IO operations (usually left to the reader) byBlockingQueue To the other thread to handle.

to sum up

  • Server-side programming, if the working set (service program in response to a request to access the memory size) is large, then use a multi-threaded process, and prevent CPU cache swapped out, affect the performance; otherwise use single-threaded multi-process, enjoy the convenience of single-threaded programming.
  • Thread can not reduce the workload of the CPU time can not be reduced.
  • Multi-threaded server-side programming is recommended Reactor + Thread pool model, take one thread per thread + Non-blocking mode.
  • The separate IO thread and thread computing, require "computing" BlockingQueue time is placed in the thread pool and to deal with "computing."

Guess you like

Origin blog.csdn.net/YoungSusie/article/details/91960074