Gio.Cancellable.connect

### Method

# Gio Cancellableconnect

since: 2.22

#### Declaration [src]

    gulong
    g_cancellable_connect (
      GCancellable* cancellable,
      GCallback callback,
      gpointer data,
      GDestroyNotify data_destroy_func
    )

#### Description [src]

Convenience function to connect to the `GCancellable::cancelled` signal. Also handles the race condition that may happen if the cancellable is cancelled right before connecting.

`callback` is called exactly once each time `cancellable` is cancelled, either directly at the time of the connect if `cancellable` is already cancelled, or when `cancellable` is cancelled in some thread. In case the cancellable is reset via `g_cancellable_reset()` then the callback can be called again if the `cancellable` is cancelled and if it had not been previously cancelled at the time `g_cancellable_connect()` was called (e.g. if the connection actually took place, returning a non-zero value).

`data_destroy_func` will be called when the handler is disconnected, or immediately if the cancellable is already cancelled.

See `GCancellable::cancelled` for details on how to use this.

Since GLib 2.40, the lock protecting `cancellable` is not held when `callback` is invoked. This lifts a restriction in place for earlier GLib versions which now makes it easier to write cleanup code that unconditionally invokes e.g. `g_cancellable_cancel()`. Note that since 2.82 GLib still holds a lock during the callback but it’s designed in a way that most of the `GCancellable` methods can be called, including `g_cancellable_cancel()` or `g_object_unref()`.

There are still some methods that will deadlock (by design) when called from the `GCancellable::cancelled` callbacks: - `g_cancellable_connect()` - `g_cancellable_disconnect()` - `g_cancellable_reset()` - `g_cancellable_make_pollfd()` - `g_cancellable_release_fd()`.

Available since: 2.22

#### Parameters

`cancellable`

  Type: `GCancellable`

  A `GCancellable`.

  The argument can be `NULL`.

`callback`

  Type: `GCallback`

  The `GCallback` to connect.

`data`

  Type: `gpointer`

  Data to pass to `callback`.

  The argument can be `NULL`.
  The data is owned by the caller of the method.

`data_destroy_func`

  Type: `GDestroyNotify`

  Free function for `data` or `NULL`.

  The argument can be `NULL`.

#### Return value

Type: `gulong`

The id of the signal handler or 0 if `cancellable` has already been cancelled.