.NET Threading - is lock needed for assignments

纵然是瞬间 提交于 2019-11-30 14:49:03

问题


I've got some multi threaded code I'd like to increase the performace of a bit, so I'm wondering if I can get rid of a lock.

I've got a field member:

private IList<ServerStatus> status;

It's updated in a thread like so:

status = GetUpdatedStatus();

And it's used in another thread like this:

var currentStatus = status;

So the question is, can the above yield any problems without locks around the two assignment statements ?

I guess the only scenario I can see is currentStatus being null, but then again I'd expect an assignment to be somewhat thread-safe (either it has changed the reference or not)


回答1:


You are right. You will see the assignment or you won't see it. Assignments (and reads) of references are always "atomic" (in the end it's because on 32 bits machines references are 32 bits, so can be done atomically, and on 64 bits machines (running a 64 bits app) references are 64 bits, so can be done atomically. The only exception is trying to write/read a long (64 bits) on a 32 bits machine. There you would have to use Interlocked.Read / Interlocked.Exchange)

Normally should declare status as volatile, so that each thread sees only the latest version. You should read this: http://www.albahari.com/threading/ it's very very good!

If you don't trust me, read the section Do We Really Need Locks and Barriers? here http://www.albahari.com/threading/part4.aspx

Ah... I was forgetting... The world HATES you, so there is a little thing to know of volatile: sometimes it doesn't work :-) :-) Read, in the same page of the other example, the section The volatile keyword, the part UNDER the red box. Notice that applying volatile doesn’t prevent a write followed by a read from being swapped, and this can create brainteasers. In the end, the only way to be sure is to use Interlocked.Exchange to write and Interlocked.CompareExchange to read something OR protect the read and the write sections with synchronization (like lock) OR fill your program with Thread.MemoryBarrier (but don't try it, you'll fail, and you won't even know why). You are guaranteed that all the reads and the writes done in the lock will be done IN the lock, not before or after.




回答2:


Reference writes are guaranteed atomic, so there are only really two things to check:

  • usage in a tight loop - might need to add volatile if you need to notice the change
  • double updates; if you are (for example) doing add/remove via reference swap, you should use Interlocked.CompareExchange to make sure you don't lose data; keep reapplying your change until you win the swap

i.e.

object snapshot, newValue;
do
{
    snapshot = field;

    // do something based on that; create a clone
    // with more/less data for example
    newValue = ...;
} while (!ReferenceEquals(
    Interlocked.CompareExchange(ref field, newValue, snapshot), snapshot));


来源:https://stackoverflow.com/questions/5087665/net-threading-is-lock-needed-for-assignments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!