---
name: "membarrier"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/membarrier"
---

# membarrier(2)

memory barrier

## NAME

**membarrier** – memory barrier

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/membarrier.h\>**

*int*\

**membarrier**(*int cmd*, *unsigned flags*, *int cpu\_id*);

## DESCRIPTION

The **membarrier** system call provides a memory barrier, ensuring ordering between memory accesses performed by different threads or processes within the same address space.

The following values for *cmd* may be specified:

- MEMBARRIER\_CMD\_QUERY

  Query supported commands. A bitmask of commands supported by the kernel is returned.

- MEMBARRIER\_CMD\_GLOBAL

  Issue a memory barrier for all threads from all processes.

- MEMBARRIER\_CMD\_SHARED

  This is an alias for MEMBARRIER\_CMD\_GLOBAL.

- MEMBARRIER\_CMD\_GLOBAL\_EXPEDITED

  Execute a memory barrier on all running threads of all processes registered with MEMBARRIER\_CMD\_REGISTER\_GLOBAL\_EXPEDITED.

- MEMBARRIER\_CMD\_REGISTER\_GLOBAL\_EXPEDITED

  Register the process to receive MEMBARRIER\_CMD\_GLOBAL\_EXPEDITED memory barriers.

- MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED

  Execute a memory barrier on each running thread belonging to the same process as the thread calling **membarrier**.

- MEMBARRIER\_CMD\_REGISTER\_PRIVATE\_EXPEDITED

  Register the process to receive MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED memory barriers.

- MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED\_SYNC\_CORE

  In addition to the guarantees provided by MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED it also executes machine-specific serialization instructions, which ensures that all possible speculative and out-of-order activities on the target CPU are fenced.

- MEMBARRIER\_CMD\_REGISTER\_PRIVATE\_EXPEDITED\_SYNC\_CORE

  Register the process to receive MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED\_SYNC\_CORE memory barriers.

- MEMBARRIER\_CMD\_GET\_REGISTRATIONS

  Return the bitmask of the currently registered receive barriers.

The following *cmd* values are defined for source compatibility but are not yet supported:

- MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED\_RSEQ

  - MEMBARRIER\_CMD\_REGISTER\_PRIVATE\_EXPEDITED\_RSEQ

  The *flags* argument must be 0. The *cpu\_id* argument is ignored.

## RETURN VALUES

If the *cmd* is MEMBARRIER\_CMD\_QUERY a bitmask of supported commands is returned. For the MEMBARRIER\_CMD\_GET\_REGISTRATIONS command, a bitmask of the registrations for the current process is returned. Otherwise, on success, **membarrier** returns 0. On error, -1 is returned and *errno* is set to indicate the error.

## ERRORS

**membarrier** may fail with the following errors:

- \[EINVAL\]

  *cmd* does not specify a valid command.

- \[EINVAL\]

  *flags* is not 0.

- \[EPERM\]

  An attempt was made to use MEMBARRIER\_CMD\_GLOBAL\_EXPEDITED, MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED, or MEMBARRIER\_CMD\_PRIVATE\_EXPEDITED\_SYNC\_CORE by a process that did not previously register use with the corresponding MEMBARRIER\_CMD\_REGISTER\_\* *cmd*.

## STANDARDS

The **membarrier** system call originated in Linux. This implementation aims to be source-compatible with the Linux implementation. Certain *cmd* and *flags* values are currently not supported by FreeBSD.

## HISTORY

The **membarrier** function was introduced in FreeBSD 14.1.
