using System.Collections.Concurrent;
public static int counter = 0;
public static string SlowHello(string ignored)
Interlocked.Increment(ref counter);
string s = $"Hello{counter}";
Console.WriteLine($"generated {s} to add to dict");
System.Threading.Thread.Sleep(2000 - (counter) * 1000);
public static ConcurrentDictionary<string,string> dict = new();
public static void DoGetOrAdd()
string s = dict.GetOrAdd("test", SlowHello);
Console.WriteLine($"got {s} from dict");
public static void Main()
var t1 = new Thread(DoGetOrAdd);
var t2 = new Thread(DoGetOrAdd);
System.Threading.Thread.Sleep(100);
Console.WriteLine($"Delegate calls: {counter}");
Console.WriteLine($"Items added to dict: {dict.Count}");