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...
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?
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...
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 ...
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.
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.
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.
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...
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?
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...
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.