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,