我只是想做一个简单但通过线程安全的布尔测试(和设置) 这样:
if(myBoolean==false) //should not lock/wait!
{
myBoolean=true;
.....
}
else
{
....
}
我考虑了以下内容(虽然可能不正确,所以请在我误解的地方纠正我)
任何人都知道以有效的方式执行此操作的类/构造是什么?
答案 0 :(得分:8)
答案 1 :(得分:6)
已经给出了答案(Intelocked.CompareExchange
),但这是我的用法示例:
private int _isDisposing;
public bool IsDisposing
{
get
{
return this._isDisposing != 0;
}
}
public void Dispose()
{
// Side note: I may want to `return` instead of `throw`
if (Interlocked.CompareExchange(ref _isDisposing, 1, 0) != 0)
throw new InvalidOperationException("Dispose was recursively called.");
try
{
Dispose(true);
GC.SuppressFinalize(this);
}
finally
{
_isDisposing = 0;
}
}
答案 2 :(得分:2)
你的问题有些含糊不清,但是你可能正在寻找Interlocked
类。
编辑:您正在寻找Interlocked.CompareExchange
。
答案 3 :(得分:1)
要进行线程安全测试并设置操作,代码必须阻止其他线程。为避免锁定不必要,您可以使用test-and-test-and-set模式:
if (something) {
lock(_sync) {
if (something) {
something = false;
...
}
}
}
第二个测试需要确保其他一些线程没有改变第一个测试和锁之间的值。
答案 4 :(得分:1)
对于馆藏,“测试和添加”我使用
/// <summary>
/// If h contains v then return true, else add v to h and return false.
/// Thread safe on h.
/// </summary>
/// <param name="h"></param>
/// <param name="v"></param>
/// <returns></returns>
bool TestAndAdd(HashSet<string> h, string v)
{
lock(h)
{
if(h.Contains(v))
{
return true;
}
h.Add(v);
return false;
}
}
然后我可以测试并设置为..
if (!TestAndAdd(usedCodes, mc.code))
答案 5 :(得分:0)
我多次遇到同样的问题,并且不认为我可以记住Interlocked.CompareAndChange()的最后一个参数是比较。 这就是我想出来的。
using System.Threading;
public class AtomicFlag
{
public const int SETVALUE = 1;
public const int RESETVALUE = 0;
/// <summary>
/// Represents the current state of the flag.
/// 0 means false (or reset).
/// 1 means true (or set).
/// </summary>
private int Value;
/// <summary>
/// Creates an atomicflag with the specified default value.
/// </summary>
/// <param name="initialValue">AtomicFlag.SETVALUE or
/// AtomicFlag.RESETVALUE. Defaults to RESETVALUE.</param>
public AtomicFlag(int initialValue = RESETVALUE)
{
Guard.AgainstUnsupportedValues<int>(initialValue, "initialValue",
new int[] { SETVALUE, RESETVALUE });
Value = initialValue;
}
public void Set()
{
Value = SETVALUE;
}
public void Reset()
{
Value = RESETVALUE;
}
public bool TestAndSet()
{
// Use Interlocked to test if the current value is RESETVALUE,
// return true and set value to SETVALUE.
//
// From Interlocked.CompareExchange help:
// public static int CompareExchange(
// ref int location1,
// int value,
// int comparand
// )
// where
// location1: The destination, whose value is compared with
// comparand and possibly replaced.
// value: The value that replaces the destination value if the
// comparison results in equality.
// comparand: The value that is compared to the value at
// location1.
return (RESETVALUE == Interlocked.CompareExchange(
ref Value, SETVALUE, RESETVALUE));
}
public bool TestAndReset()
{
// If the current value is SETVALUE, return true and change value
// to RESETVALUE.
return (SETVALUE ==
Interlocked.CompareExchange(ref Value, RESETVALUE, SETVALUE));
}
}