(root)/
glibc-2.38/
htl/
tests/
test-8.c
       1  /* Test pthread_once.
       2     Copyright (C) 2000-2023 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  
      28  pthread_once_t inc_var_once = PTHREAD_ONCE_INIT;
      29  int var;
      30  
      31  void
      32  inc_var (void)
      33  {
      34    var++;
      35  }
      36  
      37  void *
      38  thr (void *arg)
      39  {
      40    int i;
      41  
      42    for (i = 0; i < 500; i++)
      43      pthread_once (&inc_var_once, inc_var);
      44  
      45    return 0;
      46  }
      47  
      48  int
      49  main (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 (&tid[i], 0, thr, 0);
      58        if (err)
      59  	error (1, err, "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 (tid[i], &ret);
      69        if (err)
      70  	error (1, err, "pthread_join");
      71  
      72        assert (ret == 0);
      73      }
      74  
      75    assert (var == 1);
      76  
      77    return 0;
      78  }