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
|
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Lock Pathologies">
<meta name="DC.subject" content="Lock Pathologies">
<meta name="keywords" content="Lock Pathologies">
<meta name="DC.Relation" scheme="URI" content="../tbb_userguide/Mutual_Exclusion.htm">
<meta name="DC.Relation" scheme="URI" content="Atomic_Operations.htm">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="tutorial_Lock_Pathologies">
<link rel="stylesheet" type="text/css" href="../intel_css_styles.css">
<title>Lock Pathologies</title>
<xml>
<MSHelp:Attr Name="DocSet" Value="Intel"></MSHelp:Attr>
<MSHelp:Attr Name="Locale" Value="kbEnglish"></MSHelp:Attr>
<MSHelp:Attr Name="TopicType" Value="kbReference"></MSHelp:Attr>
</xml>
</head>
<body id="tutorial_Lock_Pathologies">
<!-- ==============(Start:NavScript)================= -->
<script src="..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
<script language="JavaScript1.2" type="text/javascript">WriteNavLink(1);</script>
<!-- ==============(End:NavScript)================= -->
<a name="tutorial_Lock_Pathologies"><!-- --></a>
<h1 class="topictitle1">Lock Pathologies</h1>
<div>
<p>Locks can introduce performance and correctness problems. If you are new
to locking, here are some of the problems to avoid:
</p>
<div class="section"><h2 class="sectiontitle">Deadlock</h2>
<p>Deadlock happens when threads are trying to acquire more than one
lock, and each holds some of the locks the other threads need to proceed. More
precisely, deadlock happens when:
</p>
<ul type="disc">
<li>
<p>There is a cycle of threads
</p>
</li>
<li>
<p>Each thread holds at least one lock on a mutex, and is waiting on
a mutex for which the
<em>next</em> thread in the cycle already has a lock.
</p>
</li>
<li>
<p>No thread is willing to give up its lock.
</p>
</li>
</ul>
<p>Think of classic gridlock at an intersection – each car has "acquired"
part of the road, but needs to "acquire" the road under another car to get
through. Two common ways to avoid deadlock are:
</p>
<ul type="disc">
<li>
<p>Avoid needing to hold two locks at the same time. Break your
program into small actions in which each can be accomplished while holding a
single lock.
</p>
</li>
<li>
<p>Always acquire locks in the same order. For example, if you have
"outer container" and "inner container" mutexes, and need to acquire a lock on
one of each, you could always acquire the "outer sanctum" one first. Another
example is "acquire locks in alphabetical order" in a situation where the locks
have names. Or if the locks are unnamed, acquire locks in order of the mutex’s
numerical addresses.
</p>
</li>
<li>
<p>Use atomic operations instead of locks.
</p>
</li>
</ul>
</div>
<div class="section"><h2 class="sectiontitle">Convoying</h2>
<p>Another common problem with locks is
<em>convoying</em>. Convoying occurs when the operating system interrupts
a thread that is holding a lock. All other threads must wait until the
interrupted thread resumes and releases the lock. Fair mutexes can make the
situation even worse, because if a waiting thread is interrupted, all the
threads behind it must wait for it to resume.
</p>
<p>To minimize convoying, try to hold the lock as briefly as possible.
Precompute whatever you can before acquiring the lock.
</p>
<p>To avoid convoying, use atomic operations instead of locks where
possible.
</p>
</div>
</div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="../tbb_userguide/Mutual_Exclusion.htm">Mutual Exclusion</a></div>
</div>
<div class="See Also">
<h2>See Also</h2>
<div class="linklist">
<div><a href="Atomic_Operations.htm">Atomic Operations
</a></div></div>
</div>
</body>
</html>
|