\"      $NetBSD: call_once.3,v 1.2 2019/04/27 10:57:11 wiz Exp $
\"
\" Copyright (c) 2016 The NetBSD Foundation, Inc.
\" All rights reserved.
\"
\" This code is derived from software contributed to The NetBSD Foundation
\" by Kamil Rytarowski.
\"
\" Redistribution and use in source and binary forms, with or without
\" modification, are permitted provided that the following conditions
\" are met:
\" 1. Redistributions of source code must retain the above copyright
\"    notice, this list of conditions and the following disclaimer.
\" 2. Redistributions in binary form must reproduce the above copyright
\"    notice, this list of conditions and the following disclaimer in the
\"    documentation and/or other materials provided with the distribution.
\"
\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\" POSSIBILITY OF SUCH DAMAGE.
\"
Dd October 16, 2016
Dt CALL_ONCE 3
Os
Sh NAME
Nm call_once
Nd calls function exactly once
Sh LIBRARY
Lb libpthread
Sh SYNOPSIS
In threads.h
Ft void
Fn call_once "once_flag *flag" "void (*func)(void)"
Vt #define ONCE_FLAG_INIT /* implementation specified */
Sh DESCRIPTION
The
Nm
function uses the
Fa flag
parameter to ensure that
Fa func
is called exactly once,
even if called from several threads.
Pp
The
Dv ONCE_FLAG_INIT
definition expands to a value that can be used to initialize an object of type
Dv once_flag .
Pp
This portable interface is implemented on top of the
Xr pthread_once 3
functionality.
Sh RETURN VALUES
None.
Sh EXAMPLES
The following calls
Nm
from two threads using the portable
Xr thrd 3
interface.
Bd -literal -offset indent
#include <stdio.h>
#include <threads.h>

static once_flag oflag = ONCE_FLAG_INIT;

void
called_once(void)
{
       printf("called once\n");
}

int
tfun(void *ptr)
{
       call_once(&oflag, called_once);
}

int
main(int argc, char **argv)
{
       thrd_t th1, th2;

       thrd_create(&th1, tfun, NULL);
       thrd_create(&th2, tfun, NULL);

       thrd_join(th1, NULL);
       thrd_join(th2, NULL);

       return 0;
}
Ed
Sh SEE ALSO
Xr pthread_once 3 ,
Xr threads 3
Sh STANDARDS
The
Nm
function conforms to
St -isoC-2011 .
Sh HISTORY
This interface first appeared in
Nx 9 .
Sh AUTHORS
An Kamil Rytarowski Aq Mt [email protected]