Home > other >  How to manage the need in multiple threads share a TCP connection
How to manage the need in multiple threads share a TCP connection

Time:11-04

Is writing a web application, using the TCP protocol, but a bit of a jam in the connection management, for help.
, once established, write their own a network library to maintain and manage the connection, throw to receive data after the application layer, application layer at the same time the use of the connection in another thread to send data, the network layer is detected after the connection is broken, notify the application layer, and then destroy the connection, the application layer after receiving the news network layer disconnected, do some cleaning and connect the marked as unavailable, to avoid the connection continues to send data after destruction,
So the question is coming, because the transceiver is not in a thread, is likely to send thread calls process has entered the network library function, but at that moment, receiving threads to destroy the connection, then Crash, thought that the use of smart Pointers STD: : from, to solve the above problem, but the network connection is likely to be created in the library in the application layer destruction, then across DLL release of heap memory problems, could collapse again...
What should I do?

CodePudding user response:

Add a thread safe message buffer queue, the application layer hair message, in the message queue, network thread to get messages from the queue, pay attention to the lock,

CodePudding user response:

reference 1st floor c39660570 response:
add a thread safe message buffer queue, the application layer hair message, the in the message queue, network thread to get messages from the queue, pay attention to lock,

There's no way. If the message queue and TCP connection is one-to-one, so the message queue is created when connection is established, the connection is disconnected, the message queue and the maintenance of maintenance and the connection is the same,
If all the connections public the same message queue, low efficiency is unthinkable, even used to read and write locks and hash table maintenance and connection one-to-one queue, efficiency is also can't keep up with,

CodePudding user response:

refer to the second floor hypeaches response:
Quote: refer to 1st floor c39660570 response:

Add a thread safe message buffer queue, the application layer hair message, in the message queue, network thread to get messages from the queue, pay attention to lock,

There's no way. If the message queue and TCP connection is one-to-one, so the message queue is created when connection is established, the connection is disconnected, the message queue and the maintenance of maintenance and the connection is the same,
If all the connections public the same message queue, low efficiency is unthinkable, even used to read and write locks and hash table maintenance and connection one-to-one queue, efficiency is also can't keep up with,

You're not a thread separately for each connection, are you? With epoll or iocp, can refer to the realization of the libevent, a simple open source network library,
  • Related