-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
92 lines (72 loc) · 1.57 KB
/
thread.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Threads are not synced
* program is $h1t,
*
* DO NOT USE IT
*
* It is only an example
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* this must be calculated */
long long TOTAL = 0;
void *do_work(void *arg)
{
/*
* do some long work with shared variable
*
* extract data from arg pointer
*/
int jobs = *((int *)arg);
for (int i = 0; i < jobs; i++) {
/* start of critical section */
TOTAL++;
/* end of critical section */
}
pthread_exit(0);
}
void create_threads(pthread_t *thread_ids, int *threads, int *thread_jobs)
{
for (int i = 0; i < *threads; i++) {
if (pthread_create(thread_ids+i, NULL, do_work, thread_jobs) != 0) {
perror("Error creating new thread\n");
exit(-1);
}
}
}
void join_threads(pthread_t *thread_ids, int *threads)
{
for (int i = 0; i < *threads; i++) {
pthread_join(*(thread_ids+i), NULL);
}
}
int main(int argc, char *argv[])
{
/*
* this program creates N threads,
* each increases shared int value M times
*
* ./a.out N M
*/
int threads;
int thread_jobs;
pthread_t *thread_ids;
if (argc != 3) {
perror("Wrong number of arguments\n");
printf("Usage: %s <N> <M>\n", argv[0]);
exit(-1);
}
/* load N and M values from arguments */
threads = atoi(argv[1]);
thread_jobs = atoi(argv[2]);
/* allocate memory for thread_ids array */
thread_ids = (pthread_t *)malloc(sizeof(pthread_t)*threads);
/* spawn threads */
create_threads(thread_ids, &threads, &thread_jobs);
join_threads(thread_ids, &threads);
/* result */
printf("Total=%lld\n", TOTAL);
free(thread_ids);
return 0;
}