以下方法尝试获取与某个其他线程共享的锁。如果可以在一段时间内获得锁定,则将执行一些操作。
如果在指定的时间段内无法获得锁定,我想通过生成任务在单独的线程上再次尝试整个过程。我不希望我的初始线程等待获取锁,因为在这种情况下性能很重要。但是,在此任务完成之前,我不希望再创建任何任务。换句话说,在任何给定时间都不应该运行多个任务。在任务仍在运行时对“ExecuteOrOffload”的任何后续调用都不允许创建另一个任务。
我提出了以下解决方案,我想知道我的setter方法(setFlag)是否应该使用Interlocked.Exchange来更改标志值,或者它是否有任何区别?代码在具有大量内核的计算机上运行。
void Main()
{
//I currently use this
ExecuteOrOffload(_theLock, () => { }, () => _theFlag, newVal => _theFlag = newVal, 0);
//Should I use this instead? i.e. - Does my setter need to use Interlocked.Exchange?
ExecuteOrOffload(_theLock, () => { }, () => _theFlag, newVal => Interlocked.Exchange(ref _theFlag, newVal));
}
private object _theLock = new Object();
private int _theFlag = 0;
//Flag = 0. Allow a new Task to start if needed
//Flag = 1. A Task is already running. Don't allow new tasks to be created
private void ExecuteOrOffload(object thelock, Action theAction, Func<int> getFlag, Action<int> setFlag, int timeout = 0)
{
bool acquiredLock = false;
try
{
//If the lock can be obtained, execute the Action
Monitor.TryEnter(thelock, TimeSpan.FromSeconds(timeout), ref acquiredLock);
if (acquiredLock)
{
theAction();
}
//The lock was not obtained. Either offload to a new task or do nothing
else
{
//Get the current value of the flag
var currentValue = getFlag();
//Try set the flag to 1. Only one thread should manage to do this.
var originalValue = Interlocked.CompareExchange(ref currentValue, 1, 0);
//If this thread didn't change the flag then just return.
if (originalValue == currentValue)
{
return;
}
//The thread that gets here changes the actual flag value
setFlag(1);
//...and starts a new task
Task.Factory.StartNew(() =>
{
try
{
//Retry the whole process from a Task. This time the flag is set to 1 and a new Task will not be created should the lock time out
ExecuteOrOffload(thelock, theAction, getFlag, setFlag, 2);
}
finally
{
//Task completed (either timed out or executed the action, we don't care which). Reset the flag to allow future calls to start a new Task
setFlag(0);
}
});
}
}
catch (Exception e)
{
//Log exception
}
finally
{
//If this thread took the lock, release it
if (acquiredLock)
{
Monitor.Exit(thelock);
}
}
}
答案 0 :(得分:1)
get / setFlag模式完全是个活泼的。这不安全。您未在共享变量上进行任何同步。
您正在执行的Interlocked.CompareExchange
是非共享局部变量。这从来没有帮助。我们没有的智能JIT只会优化这种互锁操作(或将其转换为栅栏)。
对共享变量执行Interlocked.CompareExchange
。在这种情况下,没有单独的商店,您的问题已得到解决。