blob: 545a1c81466f4a9c6598b638793edcf2840b3d4b (
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
|
/*
* Thread Exerciser
*
* Unlike testthread which is mainly concerned about testing thread
* semantics this test is used to exercise the thread creation and
* accounting. A version of this test found a problem with clashing
* cpu_indexes which caused a break in plugin handling.
*
* Based on the original test case by Nikolay Igotti.
*
* Copyright (c) 2020 Linaro Ltd
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int max_threads = 10;
typedef struct {
int delay;
} ThreadArg;
static void *thread_fn(void* varg)
{
ThreadArg *arg = varg;
usleep(arg->delay);
free(arg);
return NULL;
}
int main(int argc, char **argv)
{
int i;
pthread_t *threads;
if (argc > 1) {
max_threads = atoi(argv[1]);
}
threads = calloc(sizeof(pthread_t), max_threads);
for (i = 0; i < max_threads; i++) {
ThreadArg *arg = calloc(sizeof(ThreadArg), 1);
arg->delay = i * 100;
pthread_create(threads + i, NULL, thread_fn, arg);
}
printf("Created %d threads\n", max_threads);
/* sleep until roughly half the threads have "finished" */
usleep(max_threads * 50);
for (i = 0; i < max_threads; i++) {
pthread_join(threads[i], NULL);
}
printf("Done\n");
return 0;
}
|