forked from JasonPap/ReadWriteLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.c
More file actions
53 lines (50 loc) · 1.46 KB
/
demo.c
File metadata and controls
53 lines (50 loc) · 1.46 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
/*
* Project: Read-Write Lock for C
* File: demo.c
* Author: Jason Papapanagiotakis
* Github: https://github.com/JasonPap/ReadWriteLock
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "rwlock.h"
void* read_operation(void* vlock)
{
ReadWriteLock lock = (ReadWriteLock)vlock;
pthread_t id = pthread_self();
printf("Thread: %d - started, I'm a reader.\n", id);
rwl_readLock(lock);
printf("Thread: %d - locked for read.\n", id);
sleep(5);
rwl_readUnlock(lock);
printf("Thread: %d - unlocked from read locked.\n", id);
return 0;
}
void* write_operation(void* vlock)
{
sleep(1);
ReadWriteLock lock = (ReadWriteLock)vlock;
pthread_t id = pthread_self();
printf("Thread: %d - started, I'm a writer.\n", id);
rwl_writeLock(lock);
printf("Thread: %d - locked for write.\n", id);
sleep(1);
rwl_writeUnlock(lock);
printf("Thread: %d - unlocked from write locked.\n", id);
return 0;
}
int main(void)
{
pthread_t reader1, reader2, writer1;
ReadWriteLock lock;
if(rwl_init(&lock) == -1) //initialization of the lock
return -1;
printf("lock initialized.\n");
pthread_create(&reader1, NULL, read_operation, lock);
pthread_create(&reader2, NULL, read_operation, lock);
pthread_create(&writer1, NULL, write_operation, lock);
pthread_join(reader1, NULL);
pthread_join(reader2, NULL);
pthread_join(writer1, NULL);
return 0;
}