tree: ff1828129df33b57b08e43abe18fb158d6e10d32 [path history] [tgz]
  1. include/
  2. test/
  4. default.c
  5. loop.c

libasync-loop and libasync-loop-cpp

This library provides a general-purpose thread-safe message loop implementation declared in async-loop/loop.h. It must be statically linked into clients that want to use this particular message loop implementation. Note that clients can implement their own asynchronous dispatchers instead if they have more specialized needs.


Using the message loop

libasync-loop.a provides a general-purpose thread-safe message loop implementation of an asynchronous dispatcher which you can use out of box unless you need something more specialized.

See async/loop.h for details.

#include <lib/async-loop/default.h>
#include <lib/async-loop/loop.h>
#include <lib/async/task.h>      // for async_post_task()
#include <lib/async/time.h>      // for async_now()
#include <lib/async/default.h>   // for async_get_default_dispatcher()

static async_loop_t* g_loop;

int main(int argc, char** argv) {
    async_loop_create(&kAsyncLoopConfigAttachToThread, &g_loop);


    async_loop_run(g_loop, ZX_TIME_INFINITE, false);
    return 0;

void handler(async_dispatcher_t* async, async_task_t* task, zx_status_t status) {
    printf("task deadline elapsed: status=%d", status);

    // This example doesn't have much to do, so just quit here.

zx_status_t do_stuff() {
    async_dispatcher_t* async = async_get_default_dispatcher();
    async_task_t* task = calloc(1, sizeof(async_task_t));
    task->handler = handler;
    task->deadline = async_now(async) + ZX_SEC(2);
    return async_post_task(async, task);

Setting as the default async dispatcher

If the make_default_for_current_thread configuration option is set to true and is linked into the executable, the message loop will automatically register itself as the default dispatcher for the thread on which it is created.

New threads started with async_loop_start_thread() will automatically have their default dispatcher set to the message loop regardless of the value of make_default_for_current_thread.