once1.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * once1.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Create a static pthread_once and test that it calls myfunc once.
  37. *
  38. * Depends on API functions:
  39. * pthread_once()
  40. * pthread_create()
  41. */
  42. #include "test.h"
  43. pthread_once_t once = PTHREAD_ONCE_INIT;
  44. static int washere = 0;
  45. void
  46. myfunc(void)
  47. {
  48. washere++;
  49. }
  50. void *
  51. mythread(void * arg)
  52. {
  53. assert(pthread_once(&once, myfunc) == 0);
  54. return 0;
  55. }
  56. int
  57. main()
  58. {
  59. pthread_t t1, t2;
  60. assert(pthread_create(&t1, NULL, mythread, NULL) == 0);
  61. assert(pthread_create(&t2, NULL, mythread, NULL) == 0);
  62. Sleep(2000);
  63. assert(washere == 1);
  64. return 0;
  65. }