1/* Test pthread_once.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#define _GNU_SOURCE
20
21#include <pthread.h>
22#include <assert.h>
23#include <error.h>
24#include <errno.h>
25
26#define THREADS 10
27
28pthread_once_t inc_var_once = PTHREAD_ONCE_INIT;
29int var;
30
31void
32inc_var (void)
33{
34 var++;
35}
36
37void *
38thr (void *arg)
39{
40 int i;
41
42 for (i = 0; i < 500; i++)
43 pthread_once (once_control: &inc_var_once, init_routine: inc_var);
44
45 return 0;
46}
47
48int
49main (int argc, char **argv)
50{
51 error_t err;
52 int i;
53 pthread_t tid[THREADS];
54
55 for (i = 0; i < THREADS; i++)
56 {
57 err = pthread_create (newthread: &tid[i], attr: 0, start_routine: thr, arg: 0);
58 if (err)
59 error (status: 1, errnum: err, format: "pthread_create (%d)", i);
60 }
61
62 assert (thr (0) == 0);
63
64 for (i = 0; i < THREADS; i++)
65 {
66 void *ret;
67
68 err = pthread_join (th: tid[i], thread_return: &ret);
69 if (err)
70 error (status: 1, errnum: err, format: "pthread_join");
71
72 assert (ret == 0);
73 }
74
75 assert (var == 1);
76
77 return 0;
78}
79

source code of glibc/htl/tests/test-8.c