2 using System
.Threading
;
4 public class InterlockTest
12 public static int Main() {
15 InterlockTest it
= new InterlockTest ();
18 int c
= Interlocked
.Exchange (ref it
.test
, 1);
26 c
= Interlocked
.CompareExchange (ref it
.test
, 1, -2);
34 c
= Interlocked
.Exchange (ref a
, 5);
41 b
= Interlocked
.Increment (ref a
);
48 b
= Interlocked
.Decrement (ref a
);
54 string s
= IncTest ();
62 Thread
.MemoryBarrier ();
64 interlocked_regalloc1 ();
66 Console
.WriteLine ("done!");
71 public static object[] buckets
;
72 public static object segmentCache
;
74 public static int interlocked_regalloc1 () {
76 buckets
= new object [10];
78 if (buckets
[segment
] == null) {
79 object newSegment
= new Object ();
80 segmentCache
= Interlocked
.CompareExchange (ref buckets
[segment
], newSegment
, null) == null ? null : newSegment
;
85 public static string IncTest () {
86 return "A" + Interlocked
.Increment (ref s_test
);