-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Concurrency.DoNotUseThreadStaticWithInstanceFieldsRule(2.10)
Sebastien Pouliot edited this page Feb 9, 2011
·
3 revisions
Assembly: Gendarme.Rules.Concurrency
Version: 2.10
This rule will fire if an instance field is decorated with a ThreadStatic attribute. This is an error because the attribute will only work with static fields.
Bad example:
// the field isn't static so this will do nothing
[ThreadStatic]
private List<object> items;
public void Add (object item)
{
// If the field was thread safe this would ensure that each thread had
// its own copy of the list.
if (items == null) {
items = new List<object> ();
}
items.Add (item);
}
Good example:
private List<object> items = new List<object> ();
private object mutex = new object ();
// Typically some form of locking such as the code below is used to
// serialize access to instance fields. However you can also use
// Threading.Thread.Thread::AllocateNamedDataSlot or AllocateDataSlot.
public void Add (object item)
{
lock (mutex) {
items.Add (item);
}
}
- This rule is available since Gendarme 2.6
Note that this page was autogenerated (3/17/2011 9:31:58 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!