关于c#中数据的原子操作及让人郁闷的InterLocked类

首先,查书看了一下原子操作的概念,自己编了一程序试了一下,果然,在C#中除了int型的赋值支持原子操作,其他的应该都需要同步锁定。

测试代码如下:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Threading;

namespace  Test2
{
    
class Program2
    
{
        
static void Main(string[] args)
        
{
            Thread thread1 
= new Thread(new ThreadStart(ThreadFunction1));
            Thread thread2 
= new Thread(new ThreadStart(ThreadFunction2));

            thread1.IsBackground 
= true;
            thread2.IsBackground 
= true;

            thread1.Start();
            thread2.Start();

            Console.ReadLine();
        }


        
static long value = value1;
        
const long value1 = 0x1111111100000000;
        
const long value2 = 0x0000000011111111;

        
//static int value = 0;
        
//const int value1 = 0x11110000;
        
//const int value2 = 0x00001111;
        
        
static void ThreadFunction1()
        
{
            Console.WriteLine(
"Thread1 Start ...");
            
            
while(true)
            
{
                
if (value == value1)
                    value 
= value2;
                    
//Interlocked.Exchange(ref value, value2);
                else
                    value 
= value1;
                    
//Interlocked.Exchange(ref value, value1);
            }

        }


        
static void ThreadFunction2()
        
{
            Console.WriteLine(
"Thread2 Start ...");
            
//int bValue = 0;
            long bValue = 0;
            
while (true)
            
{
                
//bValue = Interlocked.Read(ref value);
                bValue = value;
                
if (bValue != value1 && bValue != value2)
                
{
                    Console.WriteLine(
"Error");
                }

            }

        }

    }

}

 不过让人郁闷的是微软的System.Threading.InterLocked类,本来想用它替换Lock方法的,但测了一下其性能,发现对long型的操作竟然比lock还要慢,倒。。。

 

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Test2
{
    
class Program
    
{
        
const long  COUNT = 100000000;
        
static long  count = COUNT;
        
static long  var = 0;
        
static object obj = new object();   // lock锁

        
static void Main(string[] args)
        
{
            
long start = 0;
            start 
= System.DateTime.Now.Ticks;
            
while (count-- > 0)
            
{
                
lock (obj)
                
{
                    var
++;
                }

            }

            
long duration = System.DateTime.Now.Ticks - start;
            Console.WriteLine(duration);

            start 
= System.DateTime.Now.Ticks;
            var 
= 0;
            count 
= COUNT;
            
while (count-- > 0)
            
{
                System.Threading.Interlocked.Increment(
ref var);
            }


            duration 
= System.DateTime.Now.Ticks - start;
            Console.WriteLine(duration);


        }

    }

}

下面是测试结果:

28750000

33437500

汗。。。不知道微软怎么处理的。因为在对int型的测试中,InterLocked比lock要快一个数量级呢。。。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值