---
name: "getcontext"
section: "3"
platform: "macos"
package: "libplatform"
version: "375.120.2"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/getcontext/3"
---

# getcontext(3)

get and set user thread context

## NAME

**getcontext**, **setcontext** – get and set user thread context

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<ucontext.h\>**

*int*\

**getcontext**(*ucontext\_t \*ucp*);

*int*\

**setcontext**(*const ucontext\_t \*ucp*);

## DESCRIPTION

The **getcontext**() function saves the current thread's execution context in the structure pointed to by *ucp*. This saved context may then later be restored by calling **setcontext**().

The **setcontext**() function makes a previously saved thread context the current thread context, i.e., the current context is lost and **setcontext**() does not return. Instead, execution continues in the context specified by *ucp*, which must have been previously initialized by a call to **getcontext**(), [makecontext(3)](/makecontext/3.md), or by being passed as an argument to a signal handler (see [sigaction(2)](/sigaction/2.md)).

If *ucp* was initialized by **getcontext**(), then execution continues as if the original **getcontext**() call had just returned (again).

If *ucp* was initialized by [makecontext(3)](/makecontext/3.md), execution continues with the invocation of the function specified to [makecontext(3)](/makecontext/3.md). When that function returns, *ucp-\>uc\_link* determines what happens next: if *ucp-\>uc\_link* is NULL, the process exits; otherwise, **setcontext**(*ucp-\>uc\_link*) is implicitly invoked.

If *ucp* was initialized by the invocation of a signal handler, execution continues at the point the thread was interrupted by the signal.

## RETURN VALUES

If successful, **getcontext**() returns zero and **setcontext**() does not return; otherwise -1 is returned.

## ERRORS

No errors are defined for **getcontext**() or **setcontext**().

## SEE ALSO

[sigaction(2)](/sigaction/2.md), [sigaltstack(2)](/sigaltstack/2.md), [makecontext(3)](/makecontext/3.md), [ucontext(3)](/ucontext/3.md)
