-
Notifications
You must be signed in to change notification settings - Fork 13
/
11_pth_posix_barrier.c
92 lines (77 loc) · 2.61 KB
/
11_pth_posix_barrier.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
92
/*****************************************************************************
* File: 11_pth_posix_barrier.c
* Purpose: Use POSIX barrier to synchronize threads
*
* Compile: gcc -Wall -o 11_pth_posix_barrier 11_pth_posix_barrier.c -pthread
* [-DDEBUG]
* Run: ./11_pth_posix_barrier <number of threads>
*
* Input: none
* Output: Time for BARRIER_COUNT barriers
*
* Note: Verbose output can be enabled with the compile flag -DDEBUG
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#define GET_TIME(now) \
{ \
struct timeval t; \
gettimeofday(&t, NULL); \
now = t.tv_sec + t.tv_usec / 1000000.0; \
}
#define BARRIER_COUNT 100
/* Global variables */
long thread_count;
pthread_barrier_t barrier;
void* Thread_work(void* rank);
int main(int argc, char* argv[])
{
pthread_t* thread_handles;
if (argc != 2) {
fprintf(stderr, "Usage: %s <number of threads>\n", argv[0]);
exit(0);
}
thread_count = strtol(argv[1], NULL, 10);
if (thread_count <= 0) {
fprintf(stderr, "The number of threads should be > 0\n");
exit(0);
}
thread_handles = (pthread_t*)malloc(thread_count * sizeof(pthread_t));
pthread_barrier_init(&barrier, NULL, thread_count);
double start, finish;
GET_TIME(start);
for (long thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], NULL, Thread_work, (void*)thread);
for (long thread = 0; thread < thread_count; thread++)
pthread_join(thread_handles[thread], NULL);
GET_TIME(finish);
printf("Elapsed time = %f seconds\n", finish - start);
pthread_barrier_destroy(&barrier);
free(thread_handles);
return 0;
}
/*****************************************************************************
* Function: Thread_work
* Purpose: Run BARRIER_COUNT barriers
* In args: rank
* Global var: thread_count, barrier
* Return: ignored(NULL)
*****************************************************************************/
void* Thread_work(void* rank)
{
#ifdef DEBUG
long my_rank = (long)rank;
#endif
for (int i = 0; i < BARRIER_COUNT; i++) {
pthread_barrier_wait(&barrier);
#ifdef DEBUG
if (my_rank == 0) {
printf("All threads completed barrier %d\n", i);
fflush(stdout);
}
#endif
}
return NULL;
}