Coding Guidelines/Asynchronous Programming: Difference between revisions

From wiki.zmanda.com
Jump to navigation Jump to search
No edit summary
 
(Previous comments were not necessary)
Line 5: Line 5:


= Usage =
= Usage =
For general usage of GMainLoop, please see the glib documentation.  Remember that Amanda requires [[Installation/Amanda's Requirements|glib-2.2.0 or higher]], so any functionality used must be present in glib-2.2.0. In order to maintain compatibility with the [[Event API]], however, there are a few caveats:
For general usage of GMainLoop, please see the glib documentation.  Remember that Amanda requires [[Installation/Amanda's Requirements|glib-2.2.0 or higher]], so any functionality used must be present in glib-2.2.0.


* Instead of <tt>g_main_loop_run(loop)</tt>, use <tt>event_loop(0)</tt>.
The default <tt>GMainLoop</tt> object is available from <tt>event.h</tt>'s <tt>default_main_loop()</tt>, so you might write
* To "break" the event loop, instead of <tt>g_main_loop_quit(loop)</tt>, use <tt>event_quit()</tt>.
g_main_loop_run(default_main_loop());
or
g_main_loop_quit(default_main_loop());

Revision as of 18:20, 25 February 2008

Asynchronous programming is a way of writing code which reacts to events, rather than churning mercilessly through a fixed process. In essence, the program consists of a "main loop" which waits for events to occur, and a dispatcher which invokes the appropriate piece of code to handle each event. This style is usually used when writing network daemons, where the events are incoming data from clients and timeouts.

History

Historically, Amanda used the Event API to handle asynchronous programming. As a general platform for structuring applications, this API was inadequate. Luckily, Glib provides a similar, but more advanced, service called GMainLoop. The Event API was rewritten as a wrapper around GMainLoop, and all subsequent code is expected to use GMainLoop directly.

Usage

For general usage of GMainLoop, please see the glib documentation. Remember that Amanda requires glib-2.2.0 or higher, so any functionality used must be present in glib-2.2.0.

The default GMainLoop object is available from event.h's default_main_loop(), so you might write

g_main_loop_run(default_main_loop());

or

g_main_loop_quit(default_main_loop());