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

# pthread\_once(3)

dynamic package initialization

## NAME

**pthread\_once** – dynamic package initialization

## LIBRARY

POSIX Threads Library (libpthread, -lpthread)

## SYNOPSIS

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

pthread\_once\_t *once\_control* = PTHREAD\_ONCE\_INIT;

*int*\

**pthread\_once**(*pthread\_once\_t \*once\_control*, *void (\*init\_routine)(void)*);

## DESCRIPTION

The first call to **pthread\_once**() by any thread in a process, with a given *once\_control*, will call the **init\_routine**() with no arguments. Subsequent calls to **pthread\_once**() with the same *once\_control* will not call the **init\_routine**(). On return from **pthread\_once**(), it is guaranteed that **init\_routine**() has completed. The *once\_control* parameter is used to determine whether the associated initialization routine has been called.

The function **pthread\_once**() is not a cancellation point. However, if **init\_routine**() is a cancellation point and is cancelled, the effect on *once\_control* is as if **pthread\_once**() was never called.

The constant *PTHREAD\_ONCE\_INIT* is defined by header ⟨pthread.h⟩.

The behavior of **pthread\_once**() is undefined if *once\_control* has automatic storage duration or is not initialized by *PTHREAD\_ONCE\_INIT*.

## RETURN VALUES

If successful, the **pthread\_once**() function will return zero. Otherwise an error number will be returned to indicate the error.

## ERRORS

None.

## STANDARDS

The **pthread\_once**() function conforms to ISO/IEC 9945-1:1996 (“POSIX.1”).
