summaryrefslogtreecommitdiff
path: root/tests/threads/test_create_join.c
blob: 6d9f66d9377da0fdd9d6fcd95d95ebc6173ed79c (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
#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;
}