1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 */
9 /* Creates two threads, one printing 10000 "a"s, the other printing
10 10000 "b"s.
11 Illustrates: thread creation, thread joining. */
12
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include "pthread.h"
17
process(void * arg)18 static void *process(void * arg)
19 {
20 int i;
21 printf("Starting process %s\n", (char *)arg);
22 for (i = 0; i < 10000; i++)
23 write(1, (char *) arg, 1);
24 return NULL;
25 }
26
27 #define sucfail(r) (r != 0 ? "failed" : "succeeded")
libc_ex1(void)28 int libc_ex1(void)
29 {
30 int pret, ret = 0;
31 pthread_t th_a, th_b;
32 void *retval;
33
34 ret += (pret = pthread_create(&th_a, NULL, process, (void *)"a"));
35 printf("create a %s %d\n", sucfail(pret), pret);
36 ret += (pret = pthread_create(&th_b, NULL, process, (void *)"b"));
37 printf("create b %s %d\n", sucfail(pret), pret);
38 ret += (pret = pthread_join(th_a, &retval));
39 printf("join a %s %d\n", sucfail(pret), pret);
40 ret += (pret = pthread_join(th_b, &retval));
41 printf("join b %s %d\n", sucfail(pret), pret);
42 return ret;
43 }
44 #include <finsh.h>
45 FINSH_FUNCTION_EXPORT(libc_ex1, example 1 for libc);
46