Should I fork or create threads when working with libevent, libuv, libev? If so, in what cases (or at what point in the program execution, for example, when processing a specific event). Or is it better to do everything in the main thread of the program? The task is your server using these technologies.
- The use of these libraries does not affect the decision whether or not to use threads / processes. It depends solely on the problem to be solved, which must be considered comprehensively, taking into account all the nuances - Mike
- @Mike could you give some examples (at least approximately) in which cases it is worth using multithreading, and in which not? I probably do not quite understand them, but in my opinion, parallelization is not necessary for them at all, so I want to clarify for myself with examples. - Bambaleila
- oneFor example, you, at the request of a client, begin to perform a long computing task, the rest of the clients are waiting at this time. Or send a request to the database, I doubt that you will be able to make friends with the database work with this asynchrony. Or you lack the number of open sockets per process. and in the end when you have a thousand connections, no matter how short the processing of each of the requests is, but processing on several cores is faster and reduces the response time - Mike
1 answer
Multithreading is needed. At a minimum, this will allow for a fuller utilization of the capabilities of modern processors, and will allow serving more clients / connections. There is no question. But which approach to use: to run worker processes with a supervisor or work in separate threads - depends on needs. In particular, libevent really wants you to use a multiprocess approach, and libev provides mechanisms for synchronization and work with many threads and works seamlessly in a multi-threaded environment.
It should be noted that synchronization is required for the internal structures of these libraries, the mechanisms of the reactors themselves (the same epoll) are often thread-safe in and of themselves.
Well, the approaches: a process or a flow ... There is a need to plan and look at your needs: it is more difficult for processes to organize interaction, but you get much greater reliability (if one process falls, it will not affect others), plus no need for synchronization within the process . With streams everything is the same, but with a different sign.