1 /* Test of thrd_create () macro.
2 Copyright (C) 2011-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
18
19 #include <config.h>
20
21 #include <threads.h>
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "macros.h"
27
28 static thrd_t main_thread_before;
29 static thrd_t main_thread_after;
30 static thrd_t worker_thread;
31
32 #define MAGIC 1266074729
33 static volatile int work_done;
34
35 static int
36 worker_thread_func (void *arg)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
37 {
38 work_done = 1;
39 return MAGIC;
40 }
41
42 int
43 main ()
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
44 {
45 main_thread_before = thrd_current ();
46
47 if (thrd_create (&worker_thread, worker_thread_func, NULL) == thrd_success)
48 {
49 int ret;
50
51 /* Check that thrd_current () has the same value before than after the
52 first call to thrd_create (). */
53 main_thread_after = thrd_current ();
54 ASSERT (memcmp (&main_thread_before, &main_thread_after,
55 sizeof (thrd_t))
56 == 0);
57
58 ASSERT (thrd_join (worker_thread, &ret) == thrd_success);
59
60 /* Check the return value of the thread. */
61 ASSERT (ret == MAGIC);
62
63 /* Check that worker_thread_func () has finished executing. */
64 ASSERT (work_done);
65
66 return 0;
67 }
68 else
69 {
70 fputs ("thrd_create failed\n", stderr);
71 return 1;
72 }
73 }