forked from bmeurer/emscripten-dbg-stories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepping-with-state-and-threads.c
71 lines (59 loc) · 1.59 KB
/
stepping-with-state-and-threads.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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct args_ty {
int *array;
int length;
int constant;
} args;
/* multiplies each element by a constant */
void multiplyByConstant(int *array, int length, int constant) {
for (int i = 0; i < length; ++i) {
array[i] *= constant;
}
}
/* adds a constant to each element */
void addConstant(int *array, int length, int constant) {
for (int i = 0; i < length; ++i) {
array[i] += constant;
}
}
/* entry point for thread */
void *threadEntryPoint(void *input) {
args *arguments = (args*) input;
multiplyByConstant(arguments->array, arguments->length, arguments->constant);
return NULL;
}
int main() {
int n = 10;
int x[n], y[n];
/* initialize x and y */
for (int i = 0; i < n; ++i) {
x[i] = i;
y[i] = n-i-1;
}
/* this variable is our reference to the thread */
pthread_t thread;
/* intialize the argument to the thread */
args *thread_args = (args*) malloc(sizeof(args));
thread_args->array = x;
thread_args->length = n;
thread_args->constant = 5;
/* create a second thread which executes threadEntryPoint */
if(pthread_create(&thread, NULL, threadEntryPoint, (void*) thread_args)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
/* add 5 to each element in y */
addConstant(y, n, 5);
/* wait for the second thread to finish */
if(pthread_join(thread, NULL)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
/* output x and y */
for (int i = 0; i < n; ++i) {
printf("x[%d] = %d, y[%d] = %d\n", i, x[i], i, y[i]);
}
return 0;
}