aboutsummaryrefslogtreecommitdiffstats
path: root/pthreads.c
blob: 87945fd7477edf97eef8c5a89b338eacc4f86201 (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
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define NUM_THREADS 100

typedef void *(* thread_func)(void *);

void *somefunction(int *arg)
{
	int loops = rand();

	for (int i = 0; i < loops; i++) {
		pthread_yield();
		for (;i < loops; i++);
	}

	printf("%d:\t%d\n", *arg, loops);

	return NULL;
}

int main(void)
{
	srand(time(NULL));

	pthread_t threads[NUM_THREADS];
	int args[NUM_THREADS];

	for (int i = 0; i < NUM_THREADS; i++) {
		args[i] = i;
		if (pthread_create(&threads[i], NULL, (thread_func)somefunction, args + i))
			return EXIT_FAILURE;
	}

	for (int i = 0; i < NUM_THREADS; i++)
		if (pthread_join(threads[i], NULL))
			return EXIT_FAILURE;

	return EXIT_SUCCESS;
}