<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FCustom_Lock</id>
		<title>Csharp/CSharp Tutorial/Thread/Custom Lock - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FCustom_Lock"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Custom_Lock&amp;action=history"/>
		<updated>2026-04-29T23:58:54Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Custom_Lock&amp;diff=6658&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Custom_Lock&amp;diff=6658&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Custom_Lock&amp;diff=6659&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Custom_Lock&amp;diff=6659&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:11Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A spin-lock==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;/*&lt;br /&gt;
Quote from&lt;br /&gt;
Professional .NET Framework 2.0 (Programmer to Programmer) (Paperback)&lt;br /&gt;
by Joe Duffy (Author)&lt;br /&gt;
&lt;br /&gt;
# Paperback: 601 pages&lt;br /&gt;
# Publisher: Wrox (April 10, 2006)&lt;br /&gt;
# Language: English&lt;br /&gt;
# ISBN-10: 0764571354&lt;br /&gt;
# ISBN-13: 978-0764571350&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
class SpinLock&lt;br /&gt;
{&lt;br /&gt;
    private int state;&lt;br /&gt;
    private EventWaitHandle available = new AutoResetEvent(false);&lt;br /&gt;
    // This looks at the total number of hardware threads available; if it&amp;quot;s&lt;br /&gt;
    // only 1, we will use an optimized code path&lt;br /&gt;
    private static bool isSingleProc = (Environment.ProcessorCount == 1);&lt;br /&gt;
    private const int outerTryCount = 5;&lt;br /&gt;
    private const int cexTryCount = 100;&lt;br /&gt;
    public void Enter(out bool taken)&lt;br /&gt;
    {&lt;br /&gt;
        // Taken is an out parameter so that we set it *inside* the critical&lt;br /&gt;
        // region, rather than returning it and permitting aborts to creep in.&lt;br /&gt;
        // Without this, the caller could take the lock, but not release it&lt;br /&gt;
        // because it didn&amp;quot;t know it had to.&lt;br /&gt;
        taken = false;&lt;br /&gt;
        while (!taken)&lt;br /&gt;
        {&lt;br /&gt;
            if (isSingleProc)&lt;br /&gt;
            {&lt;br /&gt;
                // Don&amp;quot;t busy wait on 1-logical processor machines; try&lt;br /&gt;
                // a single swap, and if it fails, drop back to EventWaitHandle.&lt;br /&gt;
                Thread.BeginCriticalRegion();&lt;br /&gt;
                taken = Interlocked.rupareExchange(ref state, 1, 0) == 0;&lt;br /&gt;
                if (!taken)&lt;br /&gt;
                    Thread.EndCriticalRegion();&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                for (int i = 0; !taken &amp;amp;&amp;amp; i &amp;lt; outerTryCount; i++)&lt;br /&gt;
                {&lt;br /&gt;
                    // Tell the CLR we&amp;quot;re in a critical region;&lt;br /&gt;
                    // interrupting could lead to deadlocks.&lt;br /&gt;
                    Thread.BeginCriticalRegion();&lt;br /&gt;
                    // Try &amp;quot;cexTryCount&amp;quot; times to CEX the state variable:&lt;br /&gt;
                    int tries = 0;&lt;br /&gt;
                    while (!(taken =&lt;br /&gt;
                        Interlocked.rupareExchange(ref state, 1, 0) == 0) &amp;amp;&amp;amp;&lt;br /&gt;
                        tries++ &amp;lt; cexTryCount)&lt;br /&gt;
                    {&lt;br /&gt;
                        Thread.SpinWait(1);&lt;br /&gt;
                    }&lt;br /&gt;
                    if (!taken)&lt;br /&gt;
                    {&lt;br /&gt;
                        // We failed to acquire in the busy spin, mark the end&lt;br /&gt;
                        // of our critical region and yield to let another&lt;br /&gt;
                        // thread make forward progress.&lt;br /&gt;
                        Thread.EndCriticalRegion();&lt;br /&gt;
                        Thread.Sleep(0);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // If we didn&amp;quot;t acquire the lock, block.&lt;br /&gt;
            if (!taken) available.WaitOne();&lt;br /&gt;
        }&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    public void Enter()&lt;br /&gt;
    {&lt;br /&gt;
        // Convenience method. Using this could be prone to deadlocks.&lt;br /&gt;
        bool b;&lt;br /&gt;
        Enter(out b);&lt;br /&gt;
    }&lt;br /&gt;
    public void Exit()&lt;br /&gt;
    {&lt;br /&gt;
        if (Interlocked.rupareExchange(ref state, 0, 1) == 1)&lt;br /&gt;
        { &lt;br /&gt;
            // We notify the waking threads inside our critical region so&lt;br /&gt;
            // that an abort doesn&amp;quot;t cause us to lose a pulse, (which could&lt;br /&gt;
            // lead to deadlocks).&lt;br /&gt;
            available.Set();&lt;br /&gt;
            Thread.EndCriticalRegion();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        SpinLock sl1 = new SpinLock();&lt;br /&gt;
        sl1.Enter();&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Acquired the spin lock&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        finally&lt;br /&gt;
        {&lt;br /&gt;
            sl1.Exit();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Released the spin lock&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Acquired the spin lock&lt;br /&gt;
Released the spin lock&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>