C
cst
i am using .net 2.0. which should have fixed the problem making double
check locking broken...
( http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx
)
this should make the code thread safe, and would make the code perform
nicely... only taking the lock when needed, but is the indexer safe to
call (2).. and further, will ContainsKey() return a reliable value in
(1)?
private volatile static Dictionary<int, int> typeOrder = new
Dictionary<int, int>();
private static readonly object typeOrderLock = new object();
internal static int GetTypeOrder(int typeId) {
if (!typeOrder.ContainsKey(typeId))
{ // (1)
lock (typeOrderLock) {
if (!typeOrder.ContainsKey(typeId)) {
// code simpified...
typeOrder.Add(typeId, 9);
}
}
}
// is it ok to deliver results from the dictionary while
there may be added items by other threads?
return
typeOrder[typeId]; // (2)
}
check locking broken...
( http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx
)
this should make the code thread safe, and would make the code perform
nicely... only taking the lock when needed, but is the indexer safe to
call (2).. and further, will ContainsKey() return a reliable value in
(1)?
private volatile static Dictionary<int, int> typeOrder = new
Dictionary<int, int>();
private static readonly object typeOrderLock = new object();
internal static int GetTypeOrder(int typeId) {
if (!typeOrder.ContainsKey(typeId))
{ // (1)
lock (typeOrderLock) {
if (!typeOrder.ContainsKey(typeId)) {
// code simpified...
typeOrder.Add(typeId, 9);
}
}
}
// is it ok to deliver results from the dictionary while
there may be added items by other threads?
return
typeOrder[typeId]; // (2)
}