Valhalla Legends Forums Archive | C/C++ Programming | Threads and Sockets

AuthorMessageTime
soccerist
If I am doing a blocking recv on a socket with one thread (waiting for data ), can another thread (using the same socket descriptor) write to the socket without problems?
April 6, 2004, 4:52 AM
TheMinistered
I would believe the answer, my friend, is yes! I would use the main thread of the program for sending data and another thread for receiving data.

Additionally, you may want to look into using event/message driven socket event notification. WSAAsyncSelect() for message driven notification, this one is a bit less effecient than the later-- but, unless you need highspeed this will work. WSAEventSelect() for event driven notification, and this one requires a few more functions and multiple threads to work.
April 6, 2004, 12:08 PM
soccerist
[quote author=TheMinistered link=board=30;threadid=6175;start=0#msg53682 date=1081253307]
WSAAsyncSelect() for message driven notification, this one is a bit less effecient than the later-- but, unless you need highspeed this will work. WSAEventSelect() for event driven notification, and this one requires a few more functions and multiple threads to work.
[/quote]
I'm using g++ in Linux, so I don't think I have those functions available. There's a function called select() that's probably similiar and works with file descriptors. I'm not sure if it would work with sockets. Anyone know?
April 6, 2004, 4:16 PM
iago
eww@posting this in 2 places..
April 6, 2004, 4:55 PM
soccerist
[quote author=iago link=board=30;threadid=6175;start=0#msg53701 date=1081270553]
eww@posting this in 2 places..
[/quote]
Two places? They are totally different questions. Please read them carefully.

This one is about corruption of data or errors encountered in a simultaneous send & recv of 2 threads on the same socket. My other post on the other forum is a design issue question.
April 6, 2004, 5:21 PM
iago
It's basically the same question - is it good to use the double thread. And I say yes, as long as you don't have while(true) {} anywhere without a pause or sleep or wait or whatever.
April 6, 2004, 5:23 PM
soccerist
[quote author=iago link=board=30;threadid=6175;start=0#msg53709 date=1081272198]
It's basically the same question - is it good to use the double thread. And I say yes, as long as you don't have while(true) {} anywhere without a pause or sleep or wait or whatever.
[/quote]
Please try to understand what I am asking. I am not asking if it's good or bad to use a double thread. I am not asking if a while(true) is bad or good, NOR if it is ok to do with or without sleeps/pauses.

My question is about 1 send and 1 recv, done at exactly the same time.
April 6, 2004, 5:44 PM
iago
[quote author=soccerist link=board=30;threadid=6175;start=0#msg53713 date=1081273472]
[quote author=iago link=board=30;threadid=6175;start=0#msg53709 date=1081272198]
It's basically the same question - is it good to use the double thread. And I say yes, as long as you don't have while(true) {} anywhere without a pause or sleep or wait or whatever.
[/quote]
Please try to understand what I am asking. I am not asking if it's good or bad to use a double thread. I am not asking if a while(true) is bad or good, NOR if it is ok to do with or without sleeps/pauses.

My question is about 1 send and 1 recv, done at exactly the same time.
[/quote]

Ok, as far as I know, there is no conflict there. But TheMinistered already anwered that one :)
April 6, 2004, 7:43 PM
Adron
[quote author=soccerist link=board=30;threadid=6175;start=0#msg53695 date=1081268199]
I'm using g++ in Linux, so I don't think I have those functions available. There's a function called select() that's probably similiar and works with file descriptors. I'm not sure if it would work with sockets. Anyone know?
[/quote]

Try poll. Sockets are file descriptors so both should work, but poll might just be better.
April 6, 2004, 9:11 PM
soccerist
Wow, Thanks a lot.

Poll sounds like something very very useful. I didn't know socket descriptors were file descriptors. (Ya learn something new every day) :D
April 7, 2004, 4:59 AM
soccerist
Oh, I forgot to ask:

How long to people normally poll for on a socket? I know the answer really varies, but I'm just curious what range of (milliseconds?) time is typical.

I will have my polling call as part of a loop anyways.
April 7, 2004, 5:38 AM
Adron
The normal way of doing it would be to have a poll running with a very long time out, perhaps many seconds. That way you yield a lot of cpu time while you're just waiting for more data to arrive.

The timeout is only needed in case nothing ever happens and you need to discover that the connection was broken or that you must send some idle packet.
April 7, 2004, 9:34 AM

Search