summaryrefslogtreecommitdiff
path: root/src/tests/readwritemutex.cpp
blob: 8cb79e5f9043c1ec25a689ece240ec31176ef7f3 (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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */
#include <bu/readwritemutex.h>
#include <bu/thread.h>
#include <bu/randomcmwc.h>
#include <bu/sio.h>

using namespace Bu;

#define CNT 15

char disp[CNT*2+1];

ReadWriteMutex mRW;
bool bRunning;

int rbase;

Mutex mDisp;

void dispUpd( char &ind, char val )
{
    mDisp.lock();
    ind = val;
    println(disp);
    mDisp.unlock();
}

class Writer : public Thread
{
public:
    Writer( int iId, char &ind ) :
        iId( iId ),
        rand( rbase+iId ),
        ind( ind )
    {
    }

    virtual ~Writer()
    {
    }

protected:
    virtual void run()
    {
        usleep( rand.rand(1000000) );
        while( bRunning )
        {
            mRW.lockWrite();
            dispUpd( ind, 'W' );
//            println("Writer %1 locking.").arg( iId );
            usleep( rand.rand(1,10)*10000 );
//            println("Writer %1 unlocking.").arg( iId );
            dispUpd( ind, ' ' );
            mRW.unlockWrite();
            usleep( rand.rand(5,10)*10000 );
        }
    }

private:
    int iId;
    RandomCmwc rand;
    char &ind;
};

class Reader : public Thread
{
public:
    Reader( int iId, char &ind ) :
        iId( iId ),
        rand( rbase-iId ),
        ind( ind )
    {
    }

    virtual ~Reader()
    {
    }

protected:
    virtual void run()
    {
        usleep( rand.rand(1000000) );
        while( bRunning )
        {
            mRW.lockRead();
            dispUpd( ind, 'R' );
//            println("Reader %1 locking.").arg( iId );
            usleep( rand.rand(1,10)*10000 );
//            println("Reader %1 unlocking.").arg( iId );
            dispUpd( ind, ' ' );
            mRW.unlockRead();
            usleep( rand.rand(5,10)*10000 );
        }
    }

private:
    int iId;
    RandomCmwc rand;
    char &ind;
};

int main()
{
    bRunning = true;
    rbase = time( NULL );

    memset( disp, 0x20, CNT*2+1 );
    disp[CNT*2] = 0;

    Thread **threads = new Thread*[CNT*2];
    for( int j = 0; j < CNT; j++ )
    {
        threads[j] = new Reader( j+1, disp[j+CNT] );
        threads[j+CNT] = new Writer( j+1, disp[j] );
    }

    for( int j = 0; j < CNT*2; j++ )
        threads[j]->start();

    sleep( 120 );
    bRunning = false;

    for( int j = 0; j < CNT*2; j++ )
    {
        threads[j]->join();
        delete threads[j];
    }

    delete[] threads;

    return 0;
}