I'm working on a project which involves two processes, let them be A and B. A has got two threads: T1 and T2.
A and B are run separately.
At a certain point T1 sends a message (it's a COM call, a search request) to B. When B has processed some data it returns by sending the result (again, it's a COM call, the search results) directly to T2. I need T1 to block until T2 has processed B's data. So far I used the following solution:
Initialization:
searchSyncSempahore = ::CreateSemaphore(NULL, 0, 1, NULL);
T1:
B_handle->search(searchString);
::WaitForSingleObject(searchSyncSempahore, INFINITE);
// Use searchResult variable
T2:
searchResult = _some_data_from_B;
::ReleaseSemaphore(searchSyncSempahore, 1, NULL);
This works so far. It works even if B is so fast that T2 calls ReleaseSemaphore before T1 reaches the WaitForSingleObject line.
My problem is: what happens if B crashes? T1 will just wait for ever. I have to specify the timeout but if it's too low when T2 receives results it will just screw up the semaphore, messing up the following search.
So how can I correctly synchronize these two threads taking into account that T2 could never be called (i.e. How can I implement "abort search")?
T2
is actually the ATL Sink thread. – Emiliano