-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithread.h
52 lines (49 loc) · 884 Bytes
/
multithread.h
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
#pragma once
#include "omp.h"
#ifdef SERGIY_IS_NOT_BITCH
class ReaderWriterLock{
omp_lock_t mutex, wrt;
int readcount;
public:
ReaderWriterLock()
{
readcount = 0;
omp_init_lock(&wrt);
omp_init_lock(&mutex);
}
~ReaderWriterLock()
{
omp_destroy_lock(&wrt);
omp_destroy_lock(&mutex);
}
void enterReader(void)
{
omp_set_lock(&mutex);
readcount++;
if (readcount == 1){
omp_set_lock(&wrt);
}
omp_unset_lock(&mutex);
}
/* ************************************************** */
void exitReader(void)
{
omp_set_lock(&mutex);
readcount--;
if (readcount == 0){
omp_unset_lock(&wrt);
}
omp_unset_lock(&mutex);
}
/* ************************************************** */
void enterWriter(void)
{
omp_set_lock(&wrt);
}
/* ************************************************** */
void exitWriter(void)
{
omp_unset_lock(&wrt);
}
};
#endif