I am currently working on a server application that should manage collection devices over the network. Because of this, we need to do a lot of parallel programming. Over time, I learned that there are three approaches to exchanging data between processing objects (threads / processes / applications). Unfortunately, all three approaches have their drawbacks .
A) You can execute a synchronous request (call of synchronous function). In this case, the caller waits until the function is processed and a response is received. For instance:
const bool convertedSuccessfully = Sync_ConvertMovie(params);
The problem is that the caller is idling. Sometimes this is not an option. For example, if the call was triggered by a UI thread, it looks like the application is blocked until a response is received, which can take a long time.
B) You can make an asynchronous request and wait for the callback . Client code can continue with everything that needs to be done.
Async_ConvertMovie(params, TheFunctionToCallWhenTheResponseArrives);
This solution has a big drawback in that the callback function necessarily works in a separate thread. The problem is that getting a response back to the caller is difficult. For example, you clicked a button in a dialog box that called the service asynchronously, but the dialog was closed when the callback returned.
void TheFunctionToCallWhenTheResponseArrives() {
This in itself is not a big problem. However, if you want to make more than one of these calls, and all of them depend on the answer of the previous one, it becomes uncontrollably complicated in my experience.
C) The last parameter that I see is to make an asynchronous request and continue polling until a response arrives. Between the checks received when receiving the answer, you can do something useful. This is the best solution I know to solve a case in which there is a sequence of calls to asynchronous functions. This is because it has a big advantage in that you still have the whole caller context when the answer arrives. In addition, the logical sequence of calls remains reasonably clear. For instance:
const CallHandle c1 = Sync_ConvertMovie(sourceFile, destFile); while(!c1.ResponseHasArrived()) { //... do something in the meanwhile } if (!c1.IsSuccessful()) return; const CallHandle c2 = Sync_CopyFile(destFile, otherLocation); while(!c1.ResponseHasArrived()) { //... do something in the meanwhile } if (c1.IsSuccessful()) //show a success dialog
The problem with this third solution is that you cannot return from the caller function. This makes it unusable if the work you want to do between them has nothing to do with the work that you do asynchronously. For a long time, I have been wondering if there is another possibility to call functions asynchronously, and this does not have the drawbacks of the options listed above. Does anyone have an idea, some kind of clever trick?
Note: the above example is a C ++ alias. However, I think this question applies equally to C # and Java, and probably to many other languages.