-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthreads.c
100 lines (70 loc) · 1.73 KB
/
pthreads.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
93
94
95
96
97
98
99
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/*
README
First, generate a file called
pthreads_include.c
using the function "showpThread" from Program.hs. You get an example by running
Fldspr.main
Then, compile this file by running
gcc -O2 -pthread -std=gnu99 pthreads.c
on the command line. Finally, execute the program by running
./a.out
on the command line.
*/
#define NUM_THREADS 4
/* --------------------------------------------------------------------------
-- synchronization
*/
pthread_barrier_t sync_barr;
void sync_init( int numThreads )
{
pthread_barrier_init(&sync_barr, NULL, numThreads );
}
void sync()
{
int ret = pthread_barrier_wait(&sync_barr);
if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
{
printf("FATAL: Could not wait on barrier\n");
exit(-1);
}
}
/* -------------------------------------------------------------------------- */
#define ARR_SIZE 11000000
int arr[ARR_SIZE];
int arr_size = ARR_SIZE;
int result[ARR_SIZE];
int f(int x)
{
int i;
for ( i = 0; i < 10000; i++ )
x = 1-x;
return x;
}
#define min(x,y) ((x) > (y) ? (y) : (x))
#define max(x,y) ((x) > (y) ? (x) : (y))
typedef struct {
int tid;
int numThreads;
int *mem;
} arg_struct;
#include "pthreads_include.c"
/* -------------------------------------------------------------------------- */
int main (int argc, char *argv[])
{
int t;
/* data init */
for (t=0; t<ARR_SIZE; t++)
arr[t] = t;
/* start threads */
cykel_start( NUM_THREADS );
/* print results */
/* for (t=0; t < ARR_SIZE; t++)
printf("%d ", result[t]);
printf("\n"); */
/* finishing */
pthread_exit(NULL);
}
/* -------------------------------------------------------------------------- */