-
Notifications
You must be signed in to change notification settings - Fork 56
/
DecimalTotal.cs
48 lines (44 loc) · 998 Bytes
/
DecimalTotal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Threading;
namespace Synchronization
{
public class DecimalTotal
{
private decimal _total;
private SpinLock _lock;
public decimal Total
{
get
{
bool acquiredLock = false;
try
{
_lock.Enter(ref acquiredLock);
return _total;
}
finally
{
if (acquiredLock)
{
_lock.Exit();
}
}
}
}
public void Add(decimal value)
{
bool acquiredLock = false;
try
{
_lock.Enter(ref acquiredLock);
_total += value;
}
finally
{
if (acquiredLock)
{
_lock.Exit();
}
}
}
}
}