<p>When trying to implement an asynchronous API calls / Non-blocking calls, I know a little in a All Plain-C application I have, I read a about APM (Asynchronous Programming Model) by 'Delegates'. Basically what I want to do is call one API <code>f1()</code> to do a functionality(which takes long time 8-10 seconds), So I call that API <code>f1()</code>, forget about it, and continue doing some other work, e.g.  I/O for to fetch data for next call of the <code>f1()</code> or some functionality not dependent on result of <code>f1()</code>.</p>

<p>If any one has used that APM model of programming, I am looking at some concise explanation for implementing non-blocking calls. </p>

<p>Is there any other way of implementing asynchronous APIs , any other library/framework which might help in this?</p>

[{'answer_id': 62372, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 0, 'selected': False, 'text': '<p>Replace delegates with pointers to functions in C, everything else is basically same to what you have read.</p>\n'}, {'answer_id': 62839, 'author': 'JayG', 'author_id': 5823, 'author_profile': 'https://Stackoverflow.com/users/5823', 'pm_score': 1, 'selected': False, 'text': '<p>You basically need to create a multi-threaded (or multi-process) application. The f1() API needs to spawn a thread (or process) to process the data in a separate execution space. When it completes, the f1() routine needs to signal the main process that the execution is done (signal(), message queues, etc).</p>\n'}, {'answer_id': 63209, 'author': 'Ville Laurikari', 'author_id': 7446, 'author_profile': 'https://Stackoverflow.com/users/7446', 'pm_score': 1, 'selected': False, 'text': '<p>A popular way to do asynchronous programming in a plain C programs is to use an "event loop".  There are numerous libraries that you could use.  I suggest to take a look at \n<a href="http://library.gnome.org/devel/glib/2.18/glib-The-Main-Event-Loop.html" rel="nofollow noreferrer" title="glib reference manual">glib</a>.</p>\n\n<p>Another alternative is to use multiple pre-emptive threads (one for each concurrent operation) and synchronize them with mutexes and condition variables.  However, pre-emptive threading in plain C is something I would avoid, especially if you want to write portable programs.  It\'s hard to know which library functions are re-entrant, signal handling in threaded programs is a hassle, and in general C libraries and system functions have been designed for single-threaded use.</p>\n\n<p>If you\'re planning to run your application only on one platform (like Windows) and the work done with f1() is a relatively simple thing, then threading can be OK.</p>\n'}, {'answer_id': 152688, 'author': 'VoidPointer', 'author_id': 23424, 'author_profile': 'https://Stackoverflow.com/users/23424', 'pm_score': 1, 'selected': False, 'text': "<p>If the function f1() which you are referring to is not itself implemented in a asynchronous fashion, you will need to wrap it up in its own thread yourself. When doing this, you need to be careful with regards to side effects that may be caused by that particular function being called. Many libraries are not designed in a thread-safe way and multiple concurrent invocations of functions from such libraries will lead to data corruption. In such cases, you may need to wrap up the functionality in an external worker process. For heavy lifting that you mention (8-10 seconds) that overhead may be acceptable. If you will only use the external non-threadsafe functions in one thread at a time, you may be safe.</p>\n\n<p>The problem with using any form of event-loop is that an external function which isn't aware of your loop will never yield control back to your loop. Thus, you will not get to do anything else.</p>\n"}, {'answer_id': 765123, 'author': 'Maciej Piechotka', 'author_id': 49107, 'author_profile': 'https://Stackoverflow.com/users/49107', 'pm_score': 0, 'selected': False, 'text': '<p>Well. Basically I\'ve seen 2 types of async API:</p>\n\n<ol>\n<li>Interrupt. You give a call a callback which should be performed after the call. GIO (part of previously mentioned GLib) works in such a way. It is relatively easy to program with but you usually have the thread in which the callback will be run changed (except if it is integrated with the main loop as in the case of GIO).</li>\n<li>Poll. You check if the data is available. The well-known BSD Sockets operate in such a manner. It has an advantage of not necessarily being integrated with the main loop and running callback in a specific thread.</li>\n</ol>\n\n<p>If you program for Gnome or Gtk+-based I\'d like to add that <a href="http://docs.dronelabs.com/gtask/" rel="nofollow noreferrer">GTask</a> seems to be a very nice (potentially nice? I haven\'t used it). <a href="http://live.gnome.org/Vala" rel="nofollow noreferrer">Vala</a> will have better support for GIO-like async calls.</p>\n'}]
['https://Stackoverflow.com/questions/62322', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2759376/']