-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithread_danger.c
executable file
·68 lines (55 loc) · 1.51 KB
/
multithread_danger.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
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 10
#define COUNT_PER_THREAD (1000000000 / NUM_THREADS)
long int sum = 0;
pthread_mutex_t mutex;
// Function that each thread runs
void *routine(void *arg)
{
int *thread_num = (int *)arg;
long int start = (*thread_num) * COUNT_PER_THREAD;
long int end = start + COUNT_PER_THREAD;
long int parcial = 0;
// Calculate the partial sum for each thread
for (long int i = start + 1; i <= end; i++)
{
sum = sum + i;
}
return 0;
}
int main(int argc, char *argv[])
{
// Record the start time
clock_t begin = clock();
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
// Create threads
for (int i = 0; i < NUM_THREADS; i++)
{
thread_args[i] = i;
if (pthread_create(&threads[i], NULL, routine, &thread_args[i]) != 0)
{
fprintf(stderr, "Error creating thread %d\n", i);
return 2;
}
}
// Wait for threads to finish
for (int i = 0; i < NUM_THREADS; i++)
{
if (pthread_join(threads[i], NULL) != 0)
{
fprintf(stderr, "Error joining thread %d\n", i);
return 3;
}
}
// Record the end time
clock_t end = clock();
double elapsed_time = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Elapsed time: %.4lf seconds\n", elapsed_time / NUM_THREADS);
// Print the total sum
printf("Total of sum: %ld\n", sum);
return 0;
}