这是正确的互锁同步设计吗?
我有一个获取样本的系统。我的应用程序中有多个对这些示例感兴趣的客户端线程,但获取示例的实际过程只能发生在一个上下文中。它足够快,可以阻止调用进程直到采样完成,但又足够慢,我不希望多个线程堆积请求。我想出了这个设计(精简到最少的细节):
public class Sample
{
private static Sample _lastSample;
private static int _isSampling;
public static Sample TakeSample(AutomationManager automation)
{
//Only start sampling if not already sampling in some other context
if (Interlocked.CompareExchange(ref _isSampling, 0, 1) == 0)
{
try
{
Sample sample = new Sample();
sample.PerformSampling(automation);
_lastSample = sample;
}
finally
{
//We're done sampling
_isSampling = 0;
}
}
return _lastSample;
}
private void PerformSampling(AutomationManager automation)
{
//Lots of stuff going on that shouldn't be run in more than one context at the same time
}
}
在我描述的场景中使用它安全吗?
I have a system that takes Samples. I have multiple client threads in the application that are interested in these Samples, but the actual process of taking a Sample can only occur in one context. It's fast enough that it's okay for it to block the calling process until Sampling is done, but slow enough that I don't want multiple threads piling up requests. I came up with this design (stripped down to minimal details):
public class Sample
{
private static Sample _lastSample;
private static int _isSampling;
public static Sample TakeSample(AutomationManager automation)
{
//Only start sampling if not already sampling in some other context
if (Interlocked.CompareExchange(ref _isSampling, 0, 1) == 0)
{
try
{
Sample sample = new Sample();
sample.PerformSampling(automation);
_lastSample = sample;
}
finally
{
//We're done sampling
_isSampling = 0;
}
}
return _lastSample;
}
private void PerformSampling(AutomationManager automation)
{
//Lots of stuff going on that shouldn't be run in more than one context at the same time
}
}
Is this safe for use in the scenario I described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它看起来很安全,因为
int
在这里是一个原子类型。但我仍然建议替换为
和 use :
仅仅是因为它是推荐的模式,并且还确保对 _lastSample 的所有访问都得到正确处理。
注意:我希望速度相当,锁使用内部使用 Interlocked 的托管 Monitor 类。
编辑:
我错过了退避方面,这是另一个版本:
Yes, it looks safe because
int
is an atomic type here. But I would still advice replacingwith
and use :
Simply because it is the recommended pattern and also makes sure all access to _lastSample is treated correctly.
NB: I would expect comparable speed, lock uses the managed Monitor class that uses Interlocked internally.
Edit:
I missed the back-off aspect, here is another version :
我通常声明一个 volatile bool 并执行类似的操作:
I usually declare a volatile bool and do something like: