MSpec:如何使静态变量线程安全的? [英] MSpec: How to make static variables thread-safe?

查看:140
本文介绍了MSpec:如何使静态变量线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用MSpec我的最新项目,总的来说,我与它真的很高兴。不过,我有一个问题并发的时候我的测试中paralel运行,如果任何人有更好碰上这种问题,或者,甚至我不知道,有一个解决方案吗?

I'm using MSpec for my latest project, and overall I'm really happy with it. However, I do have an issue with concurrency when my tests run in paralel and I'm wondering if anybody has run into this issue or, even better, has a solution?

MSpec在很大程度上依赖于静态方法和变量工作。

MSpec heavily relies on static methods and variables to work.

当我在我的基类,由多个测试类用来定义静态变量,现在看来,我在paralel运行我的测试,它们共享相同的静态变量,从而干扰海誓山盟。

Now it appears when I define static variables in my base classes, that are used by multiple test classes, and I run my tests in paralel, that they share the same static variables and thus interfere with eachother.

我同时使用NCrunch和ReSharper的是我testrunners和我遇到两个问题。

I'm using both NCrunch and Resharper as my testrunners and I'm experiencing the problem in both.

任何人都熟悉这个问题。

Anybody familiar with this problem?

推荐答案

首先,我建议阅读的在MSDN上 THEAD安全指南。这会给你的如何以及为什么做方法的线程在C#中安全的一个很好的概述。

Firstly, I would recommend reading the Thead Safety Guidelines on MSDN. This will give you a good overview of how and why to make methods thread safe in C#.

以下规则概述为实现设计指引螺纹:

The following rules outline the design guidelines for implementing threading:


  • 避免提供了改变静态静态方法。在常见的服务器方案,静态状态在请求共享,这意味着多个线程可以同时执行该代码。这开辟了线程错误的可能性。请考虑使用封装的数据转换成不跨请求共享的实例的设计模式。

  • ...添加锁来创建线程安全的代码会降低性能,增加了锁争用,并创造了可能性死锁错误发生

  • 要注意方法的调用锁定部分。可能会导致死锁时,A类的静态方法调用B类,反之亦然静态方法。如果A和B都同步它们的静态方法,这将导致死锁。您可能只在重压力穿线发现这个僵局。

  • 要注意与lock语句(在Visual Basic中为SyncLock)问题。这是很有诱惑力的使用lock语句来解决所有线程问题。然而,System.Threading.Interlocked类是上级更新必须是原子的...

  • Avoid providing static methods that alter static state. In common server scenarios, static state is shared across requests, which means multiple threads can execute that code at the same time. This opens up the possibility for threading bugs. Consider using a design pattern that encapsulates data into instances that are not shared across requests.
  • ... Adding locks to create thread-safe code decreases performance, increases lock contention, and creates the possibility for deadlock bugs to occur
  • Be aware of method calls in locked sections. Deadlocks can result when a static method in class A calls static methods in class B and vice versa. If A and B both synchronize their static methods, this will cause a deadlock. You might discover this deadlock only under heavy threading stress.
  • Be aware of issues with the lock statement (SyncLock in Visual Basic). It is tempting to use the lock statement to solve all threading problems. However, the System.Threading.Interlocked Class is superior for updates that must be atomic ...

一般的纸条,我更喜欢的方法使用(如果可能)是使一个方法(静态或其他方式)的一成不变。要做到这一点,所有的变量应该是局部(在栈上创建本地,或者作为参数传递给方法)。通过确保只有局部变量的使用,或者成员变量是不可变的每个线程都在自己的舱室操作和改变变量将不会影响到另一个线程。这是我在.NET模拟软件广泛用于允许的方法锁定少,在C#中,因此高性能的多线程。

As a general note a methodology which I prefer to use (where possible) is to make a method (static or otherwise) immutable. To do this, all variables should be local (created locally on the stack, or passed in as parameters to a method). By ensuring only local variables are used, or member variables are immutable each thread will operate in its own compartment and changes to variables will not affect another thread. This is a methodology I have used extensively in .NET simulation software to allow lock-less and therefore high performance multithreading in C#.

另外,如果变量必须是成员变量,对他们的访问可变,可以通过锁定关键字保护。小心使用锁会导致上下文切换(减速),并介绍了死锁情况的可能性。它也不会gaurantee线程安全的使用锁必须防止您试图阻止特定的场景。

Alternatively, if variables must be member variables and mutable access to them may be protected by lock keywords. Be careful with the use of lock will cause context switching (slow down) and introduces the possibility of a deadlock situation. It also doesn't gaurantee thread safety as the use of lock must protect against the specific scenario you are trying to prevent.

有关进一步的阅读我建议看其描述在C#中的线程安全性和永恒性,这些相关的问题:

For further reading I would suggest looking these related questions which describe thread safety and immutability in C#:

  • Designing a Thread Safe class
  • Achieving Thread Safety
  • Why are immutable objects thread safe

最好的问候,

这篇关于MSpec:如何使静态变量线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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