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

# makecontext(3)

modify and exchange user thread contexts

## NAME

**makecontext**, **swapcontext** – modify and exchange user thread contexts

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

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

*void*\

**makecontext**(*ucontext\_t \*ucp*, *void \*func*, *int argc*, *...*);

*int*\

**swapcontext**(*ucontext\_t \*oucp*, *const ucontext\_t \*ucp*);

## DESCRIPTION

The **makecontext**() function modifies the user thread context pointed to by *ucp*, which must have previously been initialized by a call to [getcontext(3)](/getcontext/3.md) and had a stack allocated for it. The context is modified so that it will continue execution by invoking **func**() with the arguments (of type *int*) provided. The *argc* argument must be equal to the number of additional arguments provided to **makecontext**() and also equal to the number of arguments to **func**(), or else the behavior is undefined.

The *ucp-\>uc\_link* argument must be initialized before calling **makecontext**() and determines the action to take when **func**() returns: if equal to NULL, the process exits; otherwise, **setcontext**(*ucp-\>uc\_link*) is implicitly invoked.

The **swapcontext**() function saves the current thread context in *\*oucp* and makes *\*ucp* the currently active context.

## RETURN VALUES

If successful, **swapcontext**() returns zero; otherwise -1 is returned and the global variable *errno* is set appropriately.

## ERRORS

The **swapcontext**() function will fail if:

- \[ENOMEM\]

  There is not enough stack space in *ucp* to complete the operation.

## SEE ALSO

[setcontext(3)](/setcontext/3.md), [ucontext(3)](/ucontext/3.md)
