English 中文(简体)
如果Interlocked.Increment是原子的,为什么我要使用++?
原标题:If Interlocked.Increment is atomic, why should I ever use ++ instead?
  • 时间:2011-05-23 13:40:18
  •  标签:
  • .net

我认为这个原子操作比++更快。我只看到有利于<code>Interlocked.Increment</code>的优点。它的缺点是什么?

最佳回答

原子意味着它是线程安全的(即,一个线程不可能在另一个线程更改值时读取该值。)由于需要使用线程同步机制,这会使它变得更慢,而不是更快。如果您不关心线程安全性,则需要使用++这里是关于++运算符在不同上下文中的相对性能的一些讨论。

问题回答

原子并不意味着它更快。事实上,它几乎肯定会更慢。

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously.

这仅仅意味着在操作过程中没有可观察到的副作用。它没有说明手术需要多长时间。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签