openat: don’t close (-1)
[gnulib.git] / tests / test-thread_create.c
blobae1d331b08c3b34e4f25f71c54ac0e76cfa0c9bf
1 /* Test of gl_thread_create () macro.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
19 #include <config.h>
21 #include "glthread/thread.h"
23 #include <stdio.h>
24 #include <string.h>
26 #include "macros.h"
28 static gl_thread_t main_thread_before;
29 static gl_thread_t main_thread_after;
30 static gl_thread_t worker_thread;
32 static int dummy;
33 static volatile int work_done;
35 static void *
36 worker_thread_func (_GL_UNUSED void *arg)
38 work_done = 1;
39 return &dummy;
42 int
43 main ()
45 main_thread_before = gl_thread_self ();
47 if (glthread_create (&worker_thread, worker_thread_func, NULL) == 0)
49 void *ret;
51 /* Check that gl_thread_self () has the same value before than after the
52 first call to gl_thread_create (). */
53 main_thread_after = gl_thread_self ();
54 ASSERT (memcmp (&main_thread_before, &main_thread_after,
55 sizeof (gl_thread_t))
56 == 0);
58 gl_thread_join (worker_thread, &ret);
60 /* Check the return value of the thread. */
61 ASSERT (ret == &dummy);
63 /* Check that worker_thread_func () has finished executing. */
64 ASSERT (work_done);
66 return test_exit_status;
68 else
70 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
71 fputs ("glthread_create failed\n", stderr);
72 return 1;
73 #else
74 fputs ("Skipping test: multithreading not enabled\n", stderr);
75 return 77;
76 #endif