惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
InfoQ
N
Netflix TechBlog - Medium
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
Recorded Future
Recorded Future
J
Java Code Geeks
The Cloudflare Blog
S
Securelist
人人都是产品经理
人人都是产品经理
T
Tor Project blog
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
V
V2EX
P
Palo Alto Networks Blog
I
Intezer
罗磊的独立博客
博客园 - 叶小钗
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Hacker News
The Hacker News
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
P
Privacy International News Feed
P
Proofpoint News Feed
美团技术团队
Cisco Talos Blog
Cisco Talos Blog
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
MyScale Blog
MyScale Blog
H
Help Net Security
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 言午

Nopcommerce 二次开发2 Admin Nopcommerce 二次开发1 基础 Nopcommerce 二次开发2 WEB Nopcommerce 二次开发0 sqlce中不支持sp_rename修改表名 C#读取Excel遇到无法读取的解决方法 狼奔代码生成器 银行账户类 累 interface 抽象类 抽象方法 Message 类的继承 多态(虚方法) 事件event 委托 代理 索引! 第五周作业 第四周作业 考试! 线程安全 一
线程安全 二
言午 · 2011-11-15 · via 博客园 - 言午

using System;
using System.Threading;

abstract class Counter
{
    protected int count = 0;

    public abstract int Read(int threadNum);
    public abstract void Increment(int threadNum); 
}

// Note that the instance variable count is shared between the two methods Read
// and Increment. Threads concurrently executing one or both of these methods can
// interfere with each other unless action is taken to synchronize access

class CounterUnsafe : Counter
{
    public override int Read(int threadNum) 
    {
        // following code is not thread-safe and has a race condition
        try 
        {
            Console.WriteLine(
                "Start Resource reading (Thread={0})count: {1}", threadNum, count);
            Thread.Sleep(250);
            Console.WriteLine(
                "Stop  Resource reading (Thread={0}) count: {1}", threadNum, count);
            return count;
        }
        finally 
        {
        }
    }

    public override void Increment(int threadNum) 
    {
        // following code is not thread-safe and has a race condition
        try 
        {
            Console.WriteLine(
                "Start Resource writing (Thread={0}) count: {1}", threadNum, count);
            // the following four lines simulate count++ with a very large
            
// window of time between count being read and being incremented.
            
// This large window ensures that the race condition will create
            
// errors often when the code is accessed concurrently by multiple threads.
            int tempCount = count;
            Thread.Sleep(1000);
            tempCount++;
            count = tempCount;
            Console.WriteLine(
                "Stop  Resource writing (Thread={0}) count: {1}", threadNum, count);
        }
        finally 
        {
        }
    }
}

class CounterUsingInterlocked : Counter
{
    public override int Read(int threadNum) 
    {
        try 
        {
            // following is thread safe but since Increments can occur during
            
//  this method, the value of count can change between the start 
            
//  of the method and its return/end, if this is a problem
            
//  you need to use manual locks or a synchronized context.
            Console.WriteLine(
                "Start Resource reading (Thread={0})count: {1}", threadNum, count);
            Thread.Sleep(250);
            Console.WriteLine(
                "Stop  Resource reading (Thread={0}) count: {1}", threadNum, count);
            return count;
        }
        finally 
        {
        }
    }

    public override void Increment(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        try 
        {
            Console.WriteLine(
                "Start Resource writing (Thread={0}) count: {1}", threadNum, count);
            // Note that count++ is not  an atomic operation and therefore not thread-safe.
            
//  With count++ there would be a window of time between reading the value count and 
            
//  incrementing the value of count during which time another thread could execute and
            
//  also read and/or change the value of count. Even thought this window
            
//  is much smaller than with the CounterUnsafe methods this non atomic operation
            
//  still results in a race condition where an increment can be lost.
            
// To make the increment of count thread-safe we use an atomic Interlocked.Increment call.
            int tempCount = Interlocked.Increment(ref count);
            Console.WriteLine(
                "Stop Resource writing (Thread={0}) count: {1}", threadNum, tempCount);
        }
        finally 
        {
        }
    }
}

class CounterUsingLock : Counter
{
    public override int Read(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        lock(this)
        {
            Console.WriteLine(
                "Start Resource reading (Thread={0})count: {1}", threadNum, count);
            Thread.Sleep(250);
            Console.WriteLine(
                "Stop Resource reading (Thread={0}) count: {1}", threadNum, count);
            return count;
        }
    }

    public override void Increment(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        lock(this)
        {
            Console.WriteLine(
                "Start Resource writing (Thread={0}) count: {1}", threadNum, count);
            int tempCount = count;
            Thread.Sleep(1000);
            tempCount++;
            count = tempCount;
            Console.WriteLine(
                "Stop Resource writing (Thread={0}) count: {1}", threadNum, count);
        }
        // rest of method code that doesn't require exclusive access
    }
}

class CounterUsingMutex : Counter
{
    Mutex m = new Mutex(); 

    public override int Read(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        m.WaitOne();
        try 
        {
            Console.WriteLine(
                "Start Resource reading (Thread={0})count: {1}", threadNum, count);
            Thread.Sleep(250);
            Console.WriteLine(
                "Stop Resource reading (Thread={0}) count: {1}", threadNum, count);
            return count;
        }
        finally 
        {
            m.ReleaseMutex();
        }
    }

    public override void Increment(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        m.WaitOne();
        try 
        {
            Console.WriteLine(
                "Start Resource writing (Thread={0}) count: {1}", threadNum, count);
            int tempCount = count;
            Thread.Sleep(1000);
            tempCount++;
            count = tempCount;
            Console.WriteLine(
                "Stop Resource writing (Thread={0}) count: {1}", threadNum, count);
        }
        finally 
        {
            m.ReleaseMutex();
        }
        // rest of method code that doesn't require exclusive access
    }
}

class CounterUsingReaderWriterLock : Counter
{
    ReaderWriterLock rwl = new ReaderWriterLock();

    public override int Read(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        rwl.AcquireReaderLock(Timeout.Infinite);
        try 
        {
            Console.WriteLine(
             "Start Resource reading (Thread={0})count: {1}", threadNum, count);
            Thread.Sleep(250);
            Console.WriteLine(
             "Stop Resource reading (Thread={0}) count: {1}", threadNum, count);
            return count;
        }
        finally 
        {
            rwl.ReleaseReaderLock();
        }
    }

    public override void Increment(int threadNum) 
    {
        // ...
        
// method code that doesn't require exclusive access 
        rwl.AcquireWriterLock(Timeout.Infinite);
        try 
        {
            Console.WriteLine(
             "Start Resource writing (Thread={0}) count: {1}", threadNum, count);
            int tempCount = count;
            Thread.Sleep(1000);
            tempCount++;
            count = tempCount;
            Console.WriteLine(
             "Stop  Resource writing (Thread={0}) count: {1}", threadNum, count);
        }
        finally 
        {
            rwl.ReleaseWriterLock();
        }
        // rest of method code that doesn't require exclusive access
    }
}

class App 
{
    static Counter counter = null;
    static int totalNumberOfAsyncOps = 10;
    static int numAsyncOps = totalNumberOfAsyncOps;
    static AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);

    public static void Main() 
    {
        Console.WriteLine("\n\nUnsafe test:");
        asyncOpsAreDone.Reset();
        numAsyncOps = totalNumberOfAsyncOps;
        counter = new CounterUnsafe();
        for (int threadNum = 0; threadNum < numAsyncOps; threadNum++) 
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
        }
        asyncOpsAreDone.WaitOne();
        Console.WriteLine("All Unsafe threads have completed.");
        
        
        Console.WriteLine("\n\nInterlocked test:");
        asyncOpsAreDone.Reset();
        numAsyncOps = totalNumberOfAsyncOps;
        counter = new CounterUsingInterlocked();
        for (int threadNum = 0; threadNum < totalNumberOfAsyncOps; threadNum++) 
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
        }
        asyncOpsAreDone.WaitOne();
        Console.WriteLine("All Interlocked threads have completed.");

        Console.WriteLine("\n\nLock test:");
        asyncOpsAreDone.Reset();
        numAsyncOps = totalNumberOfAsyncOps;
        counter = new CounterUsingLock();
        for (int threadNum = 0; threadNum < numAsyncOps; threadNum++) 
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
        }
        asyncOpsAreDone.WaitOne();
        Console.WriteLine("All Lock threads have completed.");

        Console.WriteLine("\n\nMutex test:");
        asyncOpsAreDone.Reset();
        numAsyncOps = totalNumberOfAsyncOps;
        counter = new CounterUsingMutex();
        for (int threadNum = 0; threadNum < numAsyncOps; threadNum++) 
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
        }
        asyncOpsAreDone.WaitOne();
        Console.WriteLine("All Mutex threads have completed.");

    
        asyncOpsAreDone.Reset();
        numAsyncOps = totalNumberOfAsyncOps;
        counter = new CounterUsingReaderWriterLock();
        Console.WriteLine("\n\nReadWriteLock test:");
        for (int threadNum = 0; threadNum < numAsyncOps; threadNum++) 
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
        }
        asyncOpsAreDone.WaitOne();
        Console.WriteLine("All ReadWriteLock threads have completed.");
        
    }

    // The callback method's signature MUST match that of a System.Threading.TimerCallback 
    
// delegate (it takes an Object parameter and returns void)
    static void UpdateResource(Object state) 
    {
        int threadNum = (int) state;
        if ((threadNum % 2) != 0) counter.Read(threadNum);
        else counter.Increment(threadNum);

        if (( Interlocked.Decrement(ref numAsyncOps)) == 0)
            asyncOpsAreDone.Set();
    }
}