Can Threads Share the same Client Socket?
Asked Answered
S

1

8

Im using TClientSocket or indy's TIdTCPClient (depending on project)

I have a few Threads each processing items, and sometimes need to send data over the connected client socket. (Data Read form the socket is NOT used in the processing threads)

Basically my question is...

  • Is the possible?
  • is it "safe"?

or should I

  • have a client socket per thread or
  • some kinda of Marshalling/critical sections

delphi-7 indy-9

Sporting answered 10/12, 2010 at 3:4 Comment(1)
yeh i know, (kinda delphi 7 too i guess) its on the "list" of things to do ;)Sporting
C
4

Multiple threads can read and write to the same socket. Since everytime you accept, it shall extract the first connection on the queue of pending connections, create a new socket with the same socket properties and allocate a new file descriptor for that socket.

So only one thread per accepted connection.

If you are asking if you can do multiple write/read on an accepted connection, you will need locking features, hence loss of parallelism benefits. If you want to thread a long process and then write the result in the socket, use synchronization to write in the correct order.

Candlelight answered 10/12, 2010 at 3:49 Comment(1)
Thanks for that, also found this which helped tangentsoft.net/wskfaq/intermediate.html#threadsafetySporting

© 2022 - 2024 — McMap. All rights reserved.