Skip to content

Instantly share code, notes, and snippets.

@koistya
Created December 23, 2013 21:02
Show Gist options
  • Select an option

  • Save koistya/8104492 to your computer and use it in GitHub Desktop.

Select an option

Save koistya/8104492 to your computer and use it in GitHub Desktop.
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