summaryrefslogtreecommitdiff
path: root/tests/threads/test_create_join.c
blob: 12b39d31d6741221158b6b162a67e98dd897e716 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "threads/threads.h"
#include "port/unused.h"

#include <stdlib.h>		/* for EXIT_SUCCESS, EXIT_FAILURE */
#include <stdio.h>		/* for fprintf */

#define NOF_THREADS 10
#define NOF_ITERATIONS 10000

static WOLF_THREAD_RETURN_DECL thread_func( void *thread_data ) {
	int i;

	for( i = 0; i < NOF_ITERATIONS; i++ ) {
		printf( "%d\n", i );
		fflush( stdout );
	}

	WOLF_UNUSED( thread_data );

	WOLF_THREAD_RETURN
}

int main( void ) {
	int i;
	wolf_thread_t thread[NOF_THREADS];
	wolf_error_t error;
	
	for( i = 0; i < NOF_THREADS; i++ ) {
		error = wolf_thread_create( &thread[i], thread_func );
		if( error != WOLF_OK ) {
			fprintf( stderr, "Unable to start thread %d: %d\n", i, error );
			return EXIT_FAILURE;
		}
		printf( "Created thread %d\n", i );
		fflush( stdout );
	}

	for( i = 0; i < NOF_THREADS; i++ ) {
		error = wolf_thread_join( &thread[i] );
		if( error != WOLF_OK ) {
			fprintf( stderr, "Unable to join thread %d: %d\n", i, error );
			return EXIT_FAILURE;
		}
		printf( "Joined thread %d\n", i );
		fflush( stdout );
	}

	return EXIT_SUCCESS;
}