Skip to content

Commit e62cf19

Browse files
itemsInBags will be different from 500 (#8043)
* itemsInBags will be different 500 itemsInBags will be different 500 without atomic operation. * Add System.Threading package Add using System.Threading for function used.
1 parent 93804c0 commit e62cf19

File tree

1 file changed

+2
-1
lines changed
  • snippets/csharp/System.Collections.Concurrent/ConcurrentBag

1 file changed

+2
-1
lines changed

snippets/csharp/System.Collections.Concurrent/ConcurrentBag/concbag.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
6+
using System.Threading;
67

78
class ConcurrentBagDemo
89
{
@@ -36,7 +37,7 @@ static void Main()
3637
if (cb.TryTake(out item))
3738
{
3839
Console.WriteLine(item);
39-
itemsInBag++;
40+
Interlocked.Increment(ref itemsInBag);
4041
}
4142
}));
4243
}

0 commit comments

Comments
 (0)