C#多线程无符号增量 [英] C# multi-threaded unsigned increment

查看:160
本文介绍了C#多线程无符号增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲增加从多个线程无符号整数。

I want to increment an unsigned integer from multiple threads.

我知道Interlocked.Increment,但它不处理无符号整数。我可以使用锁(),但我宁愿不如果可能出于性能的考虑。

I know about Interlocked.Increment, but it does not handle unsigned integers. I could use lock(), but I would rather not if possible for performance reasons.

时的线程安全只是为了增加它以正常的方式?它不会重要,如果偶尔增量迷路了,因为它只是用于统计。我不希望是让损坏的价值。

Is it thread safe just to increment it in the normal way? It would not matter if the occasional increment got lost, as it's only used for statistics. What I don't want is the value to get corrupted.

推荐答案

您可以声明UINT为volatile。

you can declare the uint as volatile.

http://msdn.microsoft.com/en-us/library/x13ttww7(VS.71)的.aspx

这篇关于C#多线程无符号增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆