1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by: rolla.n.selbak REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license. For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 *
8 * Test that pthread_join()
9 *
10 * shall suspend the execution of the calling thread until the target
11 * 'thread' terminates, unless 'thread' has already terminated.
12 *
13 * Steps:
14 * 1. Create a new thread. Have it sleep for 3 seconds.
15 * 2. The main() thread should wait for the new thread to finish
16 * execution before exiting out.
17 *
18 */
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include "posixtest.h"
24
25 static int end_exec;
26
a_thread_func()27 static void *a_thread_func()
28 {
29 int i;
30
31 printf("Wait for 3 seconds for thread to finish execution:\n");
32 for (i = 1; i < 4; i++) {
33 printf("Waited (%d) second\n", i);
34 sleep(1);
35 }
36
37 /* Indicate that the thread has ended execution. */
38 end_exec = 1;
39
40 pthread_exit(0);
41 return NULL;
42 }
43
posix_testcase(void)44 static int posix_testcase(void)
45 {
46 pthread_t new_th;
47
48 /* Initialize flag */
49 end_exec = 0;
50
51 /* Create a new thread. */
52 if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
53 perror("Error creating thread\n");
54 return PTS_UNRESOLVED;
55 }
56
57 /* Wait for thread to return */
58 if (pthread_join(new_th, NULL) != 0) {
59 perror("Error in pthread_join()\n");
60 return PTS_UNRESOLVED;
61 }
62
63 if (end_exec == 0) {
64 printf("Test FAILED: When using pthread_join(), "
65 "main() did not wait for thread to finish "
66 "execution before continuing.\n");
67 return PTS_FAIL;
68 }
69
70 printf("Test PASSED\n");
71 return PTS_PASS;
72 }
73 #include <rtt_utest_internal.h>
74 UTEST_TC_EXPORT(testcase, "posix.pthread_join.1-1.c", RT_NULL, RT_NULL, 10);
75
76