Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket in multiple threads

I have a project where Thread A calls Accept(...) on some socket, then it passes it to another thread, which does receive on the socket and then closes the socket.

The object on which accept was called is never closed in Thread A. Is that safe?

What happens to that object in Thread A?

like image 443
Tony The Lion Avatar asked Apr 14 '26 16:04

Tony The Lion


1 Answers

A socket is not an object - it's just a number (or conceivably a pointer). If you pass an integer from one thread to another, there are no issues - the same with sockets. This assumes you are asking about OS level sockets, and not some socket class that you or a framework you are using have implemented, and that you only access the socket from one of the threads, which seems to be the case.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!