---
name: "sd_event_unref"
section: "3"
platform: "linux"
package: "libsystemd-dev"
version: "257.13-1~deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/sd_event_new"
---

# sd\_event\_unref(3)

Acquire and release an event loop object

Part of [sd\_event\_new(3)](/sd_event_new/3.md).

## NAME

sd\_event\_new, sd\_event\_default, sd\_event\_ref, sd\_event\_unref, sd\_event\_unrefp, sd\_event\_get\_tid, sd\_event - Acquire and release an event loop object

## SYNOPSIS

```
#include <systemd/sd-event.h>
```

```
typedef struct sd_event sd_event;
```

**int sd\_event\_new(sd\_event \*\****event***);**

**int sd\_event\_default(sd\_event \*\****event***);**

**sd\_event \*sd\_event\_ref(sd\_event \****event***);**

**sd\_event \*sd\_event\_unref(sd\_event \****event***);**

**void sd\_event\_unrefp(sd\_event \*\****event***);**

**int sd\_event\_get\_tid(sd\_event \****event***, pid\_t \****ret***);**

## DESCRIPTION

**sd\_event\_new()** allocates a new event loop object. The event loop object is returned in the *event* parameter. After use, drop the returned reference with **sd\_event\_unref()**. When the last reference is dropped, the object is freed.

**sd\_event\_default()** acquires a reference to the default event loop object of the calling thread, possibly allocating a new object if no default event loop object has been allocated yet for the thread. After use, drop the returned reference with **sd\_event\_unref()**. When the last reference is dropped, the event loop is freed. If this function is called while the object returned from a previous call from the same thread is still referenced, the same object is returned again, but the reference is increased by one. It is recommended to use this call instead of **sd\_event\_new()** in order to share event loop objects between various components that are dispatched in the same thread. All threads have exactly either zero or one default event loop objects associated, but never more.

After allocating an event loop object, add event sources to it with [**sd\_event\_add\_io**(3)](/sd_event_add_io/3.md), [**sd\_event\_add\_time**(3)](/sd_event_add_time/3.md), [**sd\_event\_add\_signal**(3)](/sd_event_add_signal/3.md), [**sd\_event\_add\_child**(3)](/sd_event_add_child/3.md), [**sd\_event\_add\_inotify**(3)](/sd_event_add_inotify/3.md), [**sd\_event\_add\_defer**(3)](/sd_event_add_defer/3.md), [**sd\_event\_add\_post**(3)](/sd_event_add_post/3.md) or [**sd\_event\_add\_exit**(3)](/sd_event_add_exit/3.md), and then execute the event loop using [**sd\_event\_loop**(3)](/sd_event_loop/3.md).

**sd\_event\_ref()** increases the reference count of the specified event loop object by one.

**sd\_event\_unref()** decreases the reference count of the specified event loop object by one. If the count hits zero, the object is freed. Note that it is freed regardless of whether it is the default event loop object for a thread or not. This means that allocating an event loop with **sd\_event\_default()**, then releasing it, and then acquiring a new one with **sd\_event\_default()** will result in two distinct objects. Note that, in order to free an event loop object, all remaining event sources of the event loop also need to be freed as each keeps a reference to it.

**sd\_event\_unrefp()** is similar to **sd\_event\_unref()** but takes a pointer to a pointer to an **sd\_event** object. This call is useful in conjunction with GCC's and LLVM's **Clean-up Variable Attribute**\[1\]. Note that this function is defined as inline function. Use a declaration like the following, in order to allocate an event loop object that is freed automatically as the code block is left:

```
{
        __attribute__((cleanup(sd_event_unrefp))) sd_event *event = NULL;
        int r;
        ...
        r = sd_event_default(&event);
        if (r < 0) {
          errno = -r;
          fprintf(stderr, "Failed to allocate event loop: %m\n");
        }
        ...
}
```

**sd\_event\_ref()**, **sd\_event\_unref()** and **sd\_event\_unrefp()** execute no operation if the passed in event loop object is **NULL**.

**sd\_event\_get\_tid()** retrieves the thread identifier ("TID") of the thread the specified event loop object is associated with. This call is only supported for event loops allocated with **sd\_event\_default()**, and returns the identifier for the thread the event loop is the default event loop of. See [**gettid**(2)](/gettid/2.md) for more information on thread identifiers.

## RETURN VALUE

On success, **sd\_event\_new()**, **sd\_event\_default()** and **sd\_event\_get\_tid()** return 0 or a positive integer. On failure, they return a negative errno-style error code. **sd\_event\_ref()** always returns a pointer to the event loop object passed in. **sd\_event\_unref()** always returns **NULL**.

### Errors

Returned errors may indicate the following problems:

**-ENOMEM**

Not enough memory to allocate the object.

**-EMFILE**

The maximum number of event loops has been allocated.

**-ENXIO**

**sd\_event\_get\_tid()** was invoked on an event loop object that was not allocated with **sd\_event\_default()**.

## NOTES

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

The code described here uses [**getenv**(3)](/getenv/3.md), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call [**setenv**(3)](/setenv/3.md) from a parallel thread. It is recommended to only do calls to **setenv()** from an early phase of the program when no other threads have been started.

## HISTORY

**sd\_event\_new()**, **sd\_event\_default()**, **sd\_event\_ref()**, and **sd\_event\_unref()** were added in version 213.

**sd\_event\_unrefp()** and **sd\_event\_get\_tid()** were added in version 229.

## SEE ALSO

[**systemd**(1)](/systemd/1.md), [**sd-event**(3)](/sd-event/3.md), [**sd\_event\_add\_io**(3)](/sd_event_add_io/3.md), [**sd\_event\_add\_time**(3)](/sd_event_add_time/3.md), [**sd\_event\_add\_signal**(3)](/sd_event_add_signal/3.md), [**sd\_event\_add\_child**(3)](/sd_event_add_child/3.md), [**sd\_event\_add\_inotify**(3)](/sd_event_add_inotify/3.md), [**sd\_event\_add\_defer**(3)](/sd_event_add_defer/3.md), [**sd\_event\_run**(3)](/sd_event_run/3.md), [**gettid**(2)](/gettid/2.md)

## NOTES

- 1\.

  Clean-up Variable Attribute

  https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
