overlapped socket question

Apr 21, 2009 at 11:05am
Hi everybody,

I have a multithreading application with sockets communications. One thread communicates with other machine in order to receive data. Other thread makes signal processing calculations. This task is very heavy. The processor executes both threads in a parallel manner but the problem is that if the thread that receives the data waits some instructions until the processor executes it, the receive function gets hung up. I don't want to use priorities or something like that because both tasks have high relative priority. I have read about the WSARecv function but I don't know if that solves the problem...

Can you give me some advice?

Thank you very much,

Radeberger
Apr 21, 2009 at 11:09am
Are you using TCP or UDP?
Apr 21, 2009 at 11:31am
TCP socket...
Apr 21, 2009 at 11:47am
You should have a problem reading the data then as you don't need to keep up with the network stream. It has to be something else, probably synch or buffer related.

Is there thread contention over the buffer that's being read into?
Apr 21, 2009 at 11:54am
I have no problem reading the data because I tested the application without this heavy signal processing and it works perfectly with the synchronization but if I work with the signal processing thread, the receive thread must wait in the receive function until the processor gives it the execution and in this case I have the problems because the data reception will be hung...
Apr 21, 2009 at 11:58am
A CPU bound thread should not interfere with I/O in the way you described. The Operating Systems scheduler is designed to handle this sort of thing.

How do you know the receive is failing?
Apr 21, 2009 at 12:07pm
I see I receive no more data, with wireshark and without it I can see it too because the application gets hung up...For example if I want to store in my computer the data for 5 blocks for the other machine, the applications stores the first data but with the next block of data I have problems because the other thread works parallel and this makes the receive function in the other thread wait some time ...
Apr 21, 2009 at 12:22pm
I can't pretend to understand what's going on there. Even Windows manages to do the odd bit of I/O when loaded. I take it this is C++ on Windows right? If not you might have issues with User Threads.

Wireshark's probably showing no data because the app has hung rather than the other way around.
Apr 21, 2009 at 1:10pm
I can see that I don't receive more data because I have done debugging and I see the variables...

Do you know what's going on?

Apr 21, 2009 at 2:32pm
In a word, no.

I'd need to see your code to be sure, but I suspect there's a race condition or some kind of contention between the reader and worker threads, possibly around the use of the buffer that's receiving the comms data, but not necessarily.
Apr 21, 2009 at 2:57pm
Here's what we have so far...

On host H1...
There are two thread objects H1.T1 and H1.T2 which run concurrently.
H1.T1 receives data on a socket stream from a client.
H1.T2 performs a separate task.
H1.T1 will receive all five data blocks when H1.T2 is not running.
H1.T1 will receive only one data block when H1.T2 is running.

On host H2...
There is one thread H2.T1 which connects to H1 and attempts to send five blocks of data to H1.T1.

Not much to go on really is there?

Dave
Apr 21, 2009 at 3:58pm
Hi again,

It's so Dave. I try to explain it more exactly:

thread1

while(run)
{
wait semaphore
rcv(...buffer)
release semaphore
}

thread2
{
wait semaphore
function signal processing
release semaphore
}

If I work without signal processing function the application works properly (I've tested and the synchronization is correct). But if I use the function signal processing, thread1 must wait sometimes because the processor executes this function in thread2 and at the end the programm gets hung up. I have debugged it and I think it happens because this time that thread1 had to wait for, doesn't support this ordinary recv function...

what do you think about it?

Thank you for your time guys!!
Apr 21, 2009 at 4:44pm
You've serialised your processing. That explains why you're not receiving anything.

You need to protect the buffer that you're writing in T1 and reading in T2, not the whole processing loop.
Apr 21, 2009 at 5:36pm
The buffer is protected with the semaphore...and while th2 does signal processing thread1 receives the next blocks of data....I¨m sure, the buffers are protected and it works properly but I think the receive funtion has a timeout or something like that. Can you help in this way?
Apr 21, 2009 at 7:19pm
Hi kbw,

I need to say that the code above isn't so in my programm. The thing is that this semaphores are not the same because I have other threads...but I don't want to expose all thing hier because it's to complicated. I have an application with 3 consumer/producer problem that I solve with semaphore and circular buffers...but my question this time doesn't go on this topics but with the receive function and timeout? I have problems when other process start and the receive function waits for...

Sorry for the confusion...

Radeberger
Apr 21, 2009 at 10:02pm
I understand the difficulty in explaining what you have. Lets look at it from a different perspective.

Is your receive the client end or the server end? Is it supposed to react to stuff coming in or is it reading the reply to a response it sent previously?

Is the CPU-bound worker thread doing work in response to what was read or is it independent of the comms?

DaveMortimer mentioned using a 3rd party library to handle all this, ACE in particular. I agree with him. You'll find that all sorts of issues are resolved there, stuff you've not even thought of.
Topic archived. No new replies allowed.