-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.c
88 lines (78 loc) · 2.7 KB
/
mutex.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
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
// For facility I will a struct with the mutex and the "shared" variable
// a counter that will be incremented by the threads
typedef struct SharedData {
uv_mutex_t mutex;
int counter;
} SharedData;
// The increment counter for each thread
void thread_cb (void* arg) {
// Cast the arg
SharedData* data = (SharedData*)arg;
// Grab the lock
uv_mutex_lock(&data->mutex);
// Mutate the counter
data->counter++;
// Relase the lock
uv_mutex_unlock(&data->mutex);
}
int main(int argc, char* argv[]) {
int i, r;
// Initialize our main event loop:
// See docs: http://docs.libuv.org/en/v1.x/loop.html#uv-loop-t-event-loop
uv_loop_t* loop = uv_default_loop();
// Init the mutex handle
// See docs: https://docs.libuv.org/en/v1.x/threading.html#c.uv_mutex_init
uv_mutex_t mutex_handle;
r = uv_mutex_init(&mutex_handle);
if (r < 0) {
// In case of error, print the error
// See docs: http://docs.libuv.org/en/v1.x/errors.html#c.uv_strerror
fprintf(stderr, "uv_mutex_init: %s\n", uv_strerror(r));
return r;
}
// Init and populate the SharedData struct
SharedData* data = (SharedData*)malloc(sizeof(SharedData*));
data->mutex = mutex_handle;
data->counter = 0;
// Create a array of threads
// And pass the callback function and the argument
// In our case the callback will increment our counter, that's why the struct
// is easier here, send the lock and the protected/shared resource (counter)
// See docs: https://docs.libuv.org/en/v1.x/threading.html#c.uv_thread_create
uv_thread_t threads[10];
for (i = 0; i < 10; i++) {
r = uv_thread_create(&threads[i], thread_cb, (void*)data);
if (r < 0) {
// In case of error, print the error
// See docs: http://docs.libuv.org/en/v1.x/errors.html#c.uv_strerror
fprintf(stderr, "uv_thread_create: %s\n", uv_strerror(r));
return r;
}
}
// Run the event loop
// I want to run this before the threads join
// See docs: http://docs.libuv.org/en/v1.x/loop.html#c.uv_run
r = uv_run(loop, UV_RUN_DEFAULT);
if (r < 0) {
// In case of error, print the error
// See docs: http://docs.libuv.org/en/v1.x/errors.html#c.uv_strerror
fprintf(stderr, "uv_run: %s\n", uv_strerror(r));
return r;
}
// Join the threads
for (i = 0; i < 10; i++) {
r = uv_thread_join(&threads[i]);
if (r < 0) {
// In case of error, print the error
// See docs: http://docs.libuv.org/en/v1.x/errors.html#c.uv_strerror
fprintf(stderr, "uv_thread_join: %s\n", uv_strerror(r));
return r;
}
}
// Just after everything is finished, print the counter value
printf("The counter value mutated is: %d\n", data->counter);
return r;
}