multithreading - How to keep asynchronous parallel program code manageable (for example in C++) -


I am currently working on a server application that needs to control storage devices on a network. Because of this, we need to do several parallel programming. Over time, I have learned that there are three approaches to communication between processing institutions (threads / processes / applications) Unfortunately, they have the disadvantages of all three methods .

A) You get a synchronous request (call a synchronous function). In this case, the caller waits till the function is processed and the response has been received. For example:

  const bool successfully converted = Sync_ConvertMovie (parameter);  

The problem is that the caller is useless sometimes it is not just an option. For example, if the user was called by the interface thread, it would seem that until the response The application has been blocked until it arrives, which can take a long time.

b) You can make a asynchronous request and wait for a callback . The client code can continue with whatever needs to be done.

  async_ConvertMovie (parameter, TheFunctionToCallWhenTheResponseArrives);  

This solution has a major effect that the callback function necessarily runs in a separate thread. The problem is that the caller is difficult to get back the reaction. For example, you clicked a button in a dialog that has called a service asynchronously, but the dialogue has long been closed when a callback occurs.

  When to turn on zero theresources () {// Difficulty: How to get the dialog frequency? // Difficulty 2: How to guarantee in a thread-safe way that the // dialog frequency is still valid? }  

This is not a major problem in itself, however, when you want to call more than one of these, and they all rely on the reaction of the last one, then in my experience becomes indestructible complex .

C) The last option I see is asynchronous request and keep voting until the response has not been received. The-response-out-between-the-clock, you can do something useful, this is the best solution that I know to solve the case in which the asynchronous function is a sequence of calls. This is because the big advantage of this is that when you have the answer, you have a reference to the entire caller. In addition, the logical sequence of calls is quite clear. For example:

  CONST Callhandle C1 = SyncFinfoFoto (SourceFile, destFile); While (C1.ResponseHasArrived ()) {// ... do something in the meantime} if (C1.Is successful ()) return; Const Callhandel C2 = Sync file (desktop, other location); While (C1.ResponseHasArrived ()) {// ... do something in the middle} if (c1.Is successful ()) // shows a successful dialog  

this problem The third solution is that you can not return to the function of the caller. This makes it unusable. If you want to do whatever you want to do, do not do any work you need to do in asynchronously. For a long time, I am thinking that what is the incomprehensible work There is some other possibility to call, on which the options listed above are not downside. Do anyone have any ideas, maybe some smart move?

Note: given example C ++ - such as pseudocode though, I think this question applies equally to C # and Java, and probably many others In languages

You can consider a clear "event loop" or "message loop", which is the classic method Is not very different from that, such as select a message loop for the loop or windowing system for asynchronous network functions, incoming events, may be sent to the callback when appropriate >, As in your example B, but in some cases they can be tracked separately, for example For the finite state machine causes transaction. An FSM is a great way to manage the complexity of an interaction with a protocol, which requires many steps!

An approach to designing these ideas begins with the design pattern.

Schmidt's body of work is a good starting point for these issues, if you come from C ++ background; A Python is also worthwhile from the background; And I am convinced that there are sets of similar frameworks and white papers, as you say, "many other languages" (Wikipedia url which I have given is indicated on reactor implementation for languages ​​other than ACE and Twisted Does).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -