summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/lwrb/docs/examples_src/example_thread_safety.c
blob: 5ffdee2913f0a5ecbe11601d3f5d800d440092b3 (plain)
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
/* Declare variables */
lwrb_t rb;

/* 2 mutexes, one for write operations,
    one for read operations */
mutex_t m_w, m_r;

/* 4 threads below, 2 for write, 2 for read */
void
thread_write_1(void* arg) {
    /* Use write mutex */
    while (1) {
        mutex_get(&m_w);
        lwrb_write(&rb, ...);
        mutex_give(&m_w);
    }
}

void
thread_write_2(void* arg) {
    /* Use write mutex */
    while (1) {
        mutex_get(&m_w);
        lwrb_write(&rb, ...);
        mutex_give(&m_w);
    }
}

void
thread_read_1(void* arg) {
    /* Use read mutex */
    while (1) {
        mutex_get(&m_r);
        lwrb_read(&rb, ...);
        mutex_give(&m_r);
    }
}

void
thread_read_2(void* arg) {
    /* Use read mutex */
    while (1) {
        mutex_get(&m_r);
        lwrb_read(&rb, ...);
        mutex_give(&m_r);
    }
}