-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutex.c
More file actions
60 lines (50 loc) · 1.13 KB
/
mutex.c
File metadata and controls
60 lines (50 loc) · 1.13 KB
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
/* Pthread mutex
* November 21, 2021
* Compile with -lpthread */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define THREADS 4
int counter;
pthread_mutex_t *lock;
static void *handler(void *arg) {
pthread_mutex_lock(lock);
counter++;
printf("Arg = %s\n", arg);
printf("Job %d has started\n", counter);
sleep(2);
printf("Job %d has finished\n", counter);
pthread_mutex_unlock(lock);
pthread_exit(NULL);
return NULL;
}
int main(void) {
int i, error;
pthread_t *tid = (pthread_t *)malloc((sizeof(pthread_t) * THREADS));
lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (pthread_mutex_init(lock, NULL) != 0) {
fprintf(stderr, "Cannot init mutex\n");
return 1;
}
for (i = 0; i < 4; i++) {
error = pthread_create(&tid[i], NULL, &handler, "hello");
if (error != 0) {
fprintf(stderr, "Thread can't be created: [%s]\n", strerror(error));
}
}
if (error != 0) {
free(lock);
free(tid);
return 1;
}
for (i = 0; i < 4; i++) {
pthread_join(tid[i], NULL);
}
pthread_exit(NULL);
pthread_mutex_destroy(lock);
free(lock);
free(tid);
return 0;
}