Hi,
The question is too broad. So, it depends on app design.
First, consider whether multithreading is needed, else use asynchronous io sockets.
Multithreaded sockets can introduce race conditions and deadlocks, make it difficult to access shared data securely.
Various application designs adopt different multithreaded socket models.
For example:
https://stackoverflow.com/questions/71572056/multithreaded-server-c-socket-programming
netIOManager
(could be aThreadPool
) that will only receive net data and hand it over to aMainManager
(also anotherThreadPool
) to process that data and then immediately return to its main job which is receiving net data.The
MainManager
may have further database tasks(also anotherThreadPool
) and then after processing the data it will deliver the response to thenetIoManager
so it will send the response to the client.
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.