Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Blocking sockets and multithreading  (Read 4458 times)

0 Members and 1 Guest are viewing this topic.

4th_dimention

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • http://overreactblog.blogspot.com/?expref=next-blog
Blocking sockets and multithreading
« on: March 15, 2011, 03:28:42 am »
So I am working on a Server-Client project.  The team I am on has a powerful server and we want to take advantage of all of its cores.  So we are trying to find ways to effectively use multithreading.  There are just several questions I have about the way sockets behave in multithreaded environments.

TCP sockets:
First if I have a blocking TCP receiving in one thread and want to send a message how could I do it?  Can I send the message in a different thread, or is there some way for me to force the socket to stop waiting for received messages so I can send something?

If not how exactly does a non-blocking socket work.  I haven't personally used them but another member on the team says they didn't work for him.

UDP sockets:(we are setting up two systems so we can make the TCP UDP choice later)
Secondly should we use one global UDP socket and then send all the messages with it?  We only want to do this if it's possible for two threads to send different messages simultaneously.  Otherwise we would use separate UDP sockets for each user so that there isn't any problem.

Also with UDP whether we use global or separate UDP sockets could we still have problems with the blocking receiving/sending issue?

Thank you for your help.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Blocking sockets and multithreading
« Reply #1 on: March 15, 2011, 08:27:45 am »
You could use a lock-free queue to push messages to be sent that's produced by several threads and consumed by only one.

Similar you could use another lock-free queue to produce incoming messages from one thread (with the listener socket) and consume from several others.

4th_dimention

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • http://overreactblog.blogspot.com/?expref=next-blog
Blocking sockets and multithreading
« Reply #2 on: March 16, 2011, 04:40:04 am »
I think I understand what you are saying.  (not entirely sure though.)  But the thing is, from my understanding, that might result in a sort of networking/multithreading deadlock.  Say both the server and client are waiting to receive messages.  While receiving neither side can send, and since both sides are waiting for the other to send nothing ever happens.

Maybe I am wrong though, I guess mostly it depends on whether a socket can send one message while trying to receive another.

If not another idea I had was maybe using blocking sockets that terminate frequently to give sending a chance.  This would simulate non-blocking pretty well, but first I need to find out how reliable non-blocking sockets are.  A member of the team said that he never could make them work.

Maybe I misunderstand what you were trying to say though.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Blocking sockets and multithreading
« Reply #3 on: April 04, 2011, 03:12:07 pm »
What kinda of game is it?

Just a tip: Move over to UDP. Check this site out; http://gafferongames.com/

 

anything