Switch from: Provide (24x7 uninterrupted service)
Starting point of design:
First, the network model, epoll event trigger mechanism, reactor, and proactor are similar.
Second, single thread or multi-thread
In a word, based on business needs (Io or CPU computing), multithreading can make full use of the advantages of multi-core processing, but may occupy a large amount of CPU resources.
In addition, multithreading does not increase the number of concurrent threads (restricted by the process's fd and epoll restrictions). However, smooth service can reduce the request latency.
If it is a single thread, the epoll timeout model can be fully utilized. However, a long task occupies CPU resources.
For multithreading, you can use the consumer-producer model, task queue, and thread pool.
It should be noted that the switching overhead and lock brought about by multithreading cannot be ignored. You do not need to think that you are pursuing multithreading and should design it based on the business scenario.
Third, the thread is still a process (depending on whether or not fork is used. In principle, the thread should be used whenever possible, except for fork, and the thread switching overhead is smaller than the process. In fact, the thread is generated by calling the clone system in Linux and has its own task_struct structure ).
Fourth, inter-process communication
There are shared memory, FIFO, message queue, pipe, etc. Pay attention to the shared memory, and you need to combine the lock.
We recommend that you use the TCP method. This method has many advantages, which makes it easy to deploy multiple processes on multiple servers.
Fifth, heartbeat Packet
One server needs to know the service status of the other server, so it needs to send heartbeat packets continuously.
Sixth, locks and condition Variables
Recursive lock or non-recursive lock?
The default locks are non-recursive locks. However, we recommend that you specify attributes when creating mutex to ensure code portability. We recommend that you use non-recursive locks. Recursive locks can cause unnecessary troubles and hide program bugs.
Lock encapsulation. The principle is resource acquisition is initialization.
Conditional variables must be combined with locks.
To release resources (thread cancellation point), you need to install the callback function for thread exit and protect possible code (pthread_testcancle surrounded by code)