forked from pixelb/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.html
49 lines (46 loc) · 1.25 KB
/
table.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>table</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<!--#include virtual="/head.html"-->
</head>
<body>
<p>
To compile the table example just do:<br>
<pre class="shell">
g++ -Wall -D_REENTRANT -lpthread PadThreads.cpp llist.c table.cpp table_test.cpp \
-o table_test
</pre>
<p>
Note the locking provided by the table class is quite fine grained,
allowing threads to traverse tables independently. The following
diagram succinctly describes the locking implementation I think.
<p>
<img src="table-locking.png">
<p>
Note you still have to be aware of lock inversion. I.E. if you
have more than 1 table, then threads must lock (acquire) items from the
tables in the same order. For e.g...
</p>
<pre class="snippet">
Table table1, table2;
thread1() {
table1entry->acquire();
/* try to acquire table2entry */
}
thread2() {
table2entry->acquire();
/* try to acquire table1entry */
}
</pre>
<p>
The deadlock is obvious here but usually much more subtle in real code.
So across threads, don't invert the locking hierarchy.
</p>
<p>
The following is a UML diagram of the table implementation.
</p>
<img src="table.png">
</body>
</html>