[英]Possible to use Interlocked.CompareExchange with a Bit?
I have an array of ints that track the completion of 10,000 concurrent tasks, where the value is either 1 or 0. I think it would be more efficient if this array was an array of Bits and each concurrent thread used interlocked.CompareExchange (or similar) to alter a single bit. 我有一个整数数组,该整数数组可以跟踪10,000个并发任务的完成,其值为1或0。我认为,如果此数组是一个Bits数组,并且每个并发线程使用互锁,则效率会更高。 )更改一位。
How should I approach this if Interlocked doesn't have an overload for "bit"? 如果“互锁”没有“位”的重载,我应该如何处理?
You can fake it with Interlocked.CompareExchange(ref int, int, int)
in a loop, but I don't think it would be more efficient: 您可以在一个循环中使用
Interlocked.CompareExchange(ref int, int, int)
进行伪造,但我认为这样做不会更有效:
private static void SetBit(ref int valueToUpdate, int bitToSet)
{
while (true)
{
int oldValue = valueToUpdate;
int newValue = oldValue | bitToSet;
int result = Interlocked.CompareExchange(ref valueToUpdate, newValue, oldValue);
if (result == oldValue) break;
}
}
I do not think you can do it with bits using Interlocked
. 我不认为您可以使用
Interlocked
使用位。 You would have to switch to an array of ints
, I believe, so that each bit is within its own int
. 我相信,您将不得不切换到一个
ints
数组,以便每个位都在其自己的int
。
Interlocked.CompareExchange
compares only for equality whereas you need to compare just a single bit of that int
which you cannot do unless you use some other concurrency strategy to protect other changes to the int
the bit resides within. Interlocked.CompareExchange
仅比较是否相等,而您只需要比较该int
一小部分,除非您使用其他并发策略来保护该int
所在的int
的其他更改,否则您无法进行比较。
You need thread-safe constructs to track the completion of the concurrent tasks. 您需要线程安全的构造来跟踪并发任务的完成。 Hence interlocked constructs are always faster than locks (because they don't need locks), you should use the static methods from the
Interlocked
-class with integers (0 as equivalent for false
and 1 for true
for example). 因此,互锁的构造总是比锁快(因为它们不需要锁),因此您应该使用
Interlocked
-class中的带有整数的静态方法(例如,0等同于false
,1等同于true
)。
Jeffrey Richter does the same as he discribes in his book "CLR via C#" in chapter 28 "Primitive Thread Synchronization Constructs". 杰弗里·里希特(Jeffrey Richter)所做的与他在第28章“原始线程同步构造”中的“通过C#进行CLR”中的描述相同。 He introduces own lock implementations based on Interlocked constructs in his Wintellect.PowerThreading library as well and gives an excellent explanation as well as time comparisons of his own implementations and those included in the .net framework.
他还在Wintellect.PowerThreading库中介绍了基于互锁结构的自己的锁实现,并对自己的实现以及.net框架中的实现进行了出色的解释以及时间比较。
First, no, all Interlocked functions work on memory addresses, and individual bits are not addressable. 首先,不,所有互锁功能都在存储器地址上运行,并且单个位不可寻址。
So what to do instead? 那么该怎么做呢?
I would investigate two options: 我将研究两个选择:
InterlockedIncrement
it each time a task completes. InterlockedIncrement
。 And to see if all tasks are done, just check if the value has reached 10,000 yet. I don't know which is fastest. 我不知道哪一个最快。 Benchmark it, if performance matters.
如果性能很重要,请进行基准测试。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.