Created
December 23, 2013 21:02
-
-
Save koistya/8104492 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading; | |
| public class StringCollection | |
| { | |
| private List<string> collection; | |
| public StringCollection() | |
| { | |
| Interlocked.CompareExchange(ref collection, new List<string>(), null); | |
| } | |
| public void AddString(string item) | |
| { | |
| while (true) | |
| { | |
| var original = Interlocked.CompareExchange(ref collection, null, null); | |
| var copy = original.ToList(); | |
| copy.Add(item); | |
| var result = Interlocked.CompareExchange(ref collection, copy, original); | |
| if (result == original) | |
| { | |
| break; | |
| } | |
| } | |
| } | |
| public override string ToString() | |
| { | |
| var original = Interlocked.CompareExchange(ref collection, null, null); | |
| return string.Join(",", original); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment