os-70-350

os-70-350 Commit Details


Date:2013-12-31 11:51:53 (11 years 8 months ago)
Author:Natalie Adams
Branch:master
Commit:845c3fc444480bc20a4084661152541c483f6689
Parents: e436a2660d78bc6f8b5d59df637fa1d63787bb74
Message:Adding monitor sync example

Changes:

File differences

ptypes-monitor/build.bat
1
2
3
4
@echo off
call ../vcppbuild.bat monitor "/link user32.lib ../ext/libs/ptypes.lib"
monitor.exe
pause
ptypes-monitor/build.sh
1
2
3
4
#!/bin/bash
gcc -o monitor -lptypes main.cpp
./main
ptypes-monitor/monitor.cpp
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
#include <ptypes/ptypes.h>
#include <ptypes/pasync.h>
#include <ptypes/pstreams.h>
USING_PTYPES
#define MAX 10
// Inspired on http://pages.cs.wisc.edu/~remzi/OSTEP/threads-monitors.pdf
class BoundedBuffer
{
private:
int buffer[MAX];
int fill, use;
int fullEntries;
trigger * empty;
trigger * full;
mutex m_lock;
public:
BoundedBuffer() : fullEntries(0), fill(0), use(0) { empty = new trigger(true, false); full = new trigger(true, false); }
~BoundedBuffer() { delete empty; delete full; }
void produce(int element) {
if (fullEntries == MAX)
empty->wait();
output("Produce " + itostring(element));
buffer[fill] = element;
fill = (fill + 1) % MAX;
fullEntries++;
full->signal();
}
int consume() {
if (fullEntries == 0)
full->wait();
int tmp = buffer[use];
output("Consume " + itostring(tmp));
use = (use + 1) % MAX;
fullEntries--;
empty->signal();
return tmp;
}
void output(string s)
{
scopelock l(m_lock);
pout.put(s);
pout.puteol();
}
};
BoundedBuffer bb;
class threadtest1 : public thread
{
protected:
virtual void execute() {
for(int i = 0; i < MAX; i++)
bb.produce(i);
}
virtual void cleanup() { }
public:
threadtest1() : thread(false) { }
};
class threadtest2 : public thread
{
protected:
virtual void execute() {
for(int i = 0; i < MAX; i++)
bb.consume();
}
virtual void cleanup() { }
public:
threadtest2() : thread(false) { }
};
int main()
{
threadtest1 t1;
threadtest2 t2;
t1.start();
t2.start();
t1.waitfor();
t2.waitfor();
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.15881s using 14 queries.