C Collections

cthreadcomm · Section 3

ceventloop_declare_scoped

ceventloop_declare_scoped - declare a ceventloop with automatic destruction on scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadcomm.h>
 
ceventloop_declare_scoped(name);

Declares a ceventloop variable set to CEVENTLOOP_NULL and
tags it with a cleanup attribute, so the loop is destroyed
automatically, exactly as ceventloop_destroy() would do it, when
the variable leaves its scope. You must assign the variable with
ceventloop_construct() or a ceventloop_create*() call before
you use it.
 
When the declaration and the initialisation can happen in one place,
which is the common case, use ceventloop_construct_scoped()
instead.

name  Variable name to declare.

The macro uses __attribute__((cleanup)), so it needs GCC or Clang.
Leaving the variable at CEVENTLOOP_NULL for the whole scope is
safe, because the cleanup does nothing for that value.

{
    ceventloop_declare_scoped(loop);
    char *err = NULL;
    loop = ceventloop_create(32, 1, 1, &err);
    if (!loop) { fprintf(stderr, "%s\n", err); return; }
    /* ... */
}  /* the library destroys loop automatically here */

ceventloop_declare(3), ceventloop_construct_scoped(3), ceventloop_create(3), ceventloop_destroy(3)