Home > Back-end >  Multithreaded C network programming, RAII how to prevent the socket descriptor crosstalk?
Multithreaded C network programming, RAII how to prevent the socket descriptor crosstalk?

Time:12-25

"Linux multithreaded server programming: use muduo network library" described in 4.7 socket fd crosstalk:

A thread from the request of the fd=8 received more time-consuming, it began to deal with this request, and remember to send the response results to fd=8, but in the process, fd=8 disconnected, was shut down, and despite the arrival of a new connection, happened to use the same fd=8, when a thread to complete response calculation, the results sent to fd=8, the receiver has much transformed,

And then give the solution is to use RAII:

Packed in a Socket object file descriptors, all read and write operations to this file descriptor, through this object in the object's destructor closes the file descriptor, as long as the Socket is alive, there will be no other Socket object have the same file descriptor,

I would like to ask, why use this Socket object can prevent crosstalk? If fd=8 passive closed, the Socket object does not know, a Socket object does not create or use fd=8 to initialize?
  • Related