test_pthread_join.c revision e16cb84e2324f05334d18dcf5956f20f44262b62
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <pthread.h>
29#include <stdio.h>
30#include <unistd.h>
31
32static void*
33thread1_func(void* arg)
34{
35    usleep( 2000*1000 );
36    printf("thread 1 exited\n");
37    return (void*) 0x8badf00d;
38}
39
40static void*
41thread2_func(void* arg)
42{
43    pthread_t t1 = (pthread_t)arg;
44    void* result;
45
46    pthread_join(t1, &result);
47    printf("thread2 received code %08x from thread1\n", (int)result);
48    return NULL;
49}
50
51
52static void*
53thread3_func(void* arg)
54{
55    pthread_t t1 = (pthread_t)arg;
56    void* result;
57
58    pthread_join(t1, &result);
59    printf("thread3 received code %08x from thread1\n", (int)result);
60    return NULL;
61}
62
63int main( void )
64{
65    pthread_t t1, t2, t3;
66
67    pthread_create( &t1, NULL, thread1_func, NULL );
68    pthread_create( &t2, NULL, thread2_func, (void*)t1 );
69    pthread_create( &t3, NULL, thread3_func, (void*)t1 );
70
71    pthread_join(t2, NULL);
72    pthread_join(t3, NULL);
73
74    printf("OK\n");
75    return 0;
76}
77