-
Notifications
You must be signed in to change notification settings - Fork 29
/
c11_thrd_join_main.c
45 lines (39 loc) · 1.23 KB
/
c11_thrd_join_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* This file is part of MCF Gthread.
* Copyright (C) 2022-2024 LH_Mouse. All wrongs reserved.
*
* MCF Gthread is free software. Licensing information is included in
* LICENSE.TXT as a whole. The GCC Runtime Library Exception applies
* to this file. */
#include "../mcfgthread/c11.h"
#include "../mcfgthread/exit.h"
#include <assert.h>
#include <stdio.h>
static
int
test_thrd_func(void* p)
{
thrd_sleep(&(struct timespec) { .tv_sec = 2 }, __MCF_nullptr);
thrd_t mthr = p;
fprintf(stderr, "got main thread = %p, ref = %d\n", (void*) mthr, _MCF_thread_get_ref(mthr));
int code;
int err = thrd_join(mthr, &code);
assert(err == 0);
fprintf(stderr, "main thread joined: code = %d\n", code);
// zero; main thread is foreign.
assert(code == 0);
__MCF__Exit(0);
}
int
main(void)
{
thrd_t mthr = thrd_current();
assert(mthr);
fprintf(stderr, "main thread = %p, ref = %d\n", (void*) mthr, _MCF_thread_get_ref(mthr));
thrd_t cthr;
int err = thrd_create(&cthr, test_thrd_func, mthr);
assert(err == 0);
fprintf(stderr, "new thread = %p\n", (void*) cthr);
thrd_sleep(&(struct timespec) { .tv_sec = 1 }, __MCF_nullptr);
fprintf(stderr, "main thread exiting\n");
thrd_exit(42);
}