fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9. static int counter = 0;
  10.  
  11. // I don't know when threads are created / joined, which is why I need this:
  12. static List<WeakReference<ThreadLocalValue>> allStorage =
  13. new List<WeakReference<ThreadLocalValue>>();
  14.  
  15. // The performance counter
  16. [ThreadStatic]
  17. static ThreadLocalValue local;
  18.  
  19. class ThreadLocalValue : IDisposable
  20. {
  21. public ThreadLocalValue()
  22. {
  23. // CHANGE HERE!!!
  24. lock (allStorage)
  25. {
  26. allStorage.Add(new WeakReference<ThreadLocalValue>(this));
  27. }
  28. }
  29.  
  30. public int ctr = 0;
  31.  
  32. public void Dispose()
  33. {
  34. // CHANGE HERE!!!
  35. Dispose(true);
  36. }
  37.  
  38. // CHANGE HERE!!!
  39. private void Dispose(bool disposing)
  40. {
  41. // CHANGE HERE!!!
  42. // Reset the counter
  43. int ctr2 = Interlocked.Exchange(ref ctr, 0);
  44.  
  45. // Atomic add
  46. Interlocked.Add(ref Program.counter, ctr2);
  47.  
  48. if (disposing)
  49. {
  50. GC.SuppressFinalize(this);
  51. }
  52. }
  53.  
  54. ~ThreadLocalValue()
  55. {
  56. // CHANGE HERE!!!
  57. // Make sure it's merged.
  58. Dispose(false);
  59. }
  60. }
  61.  
  62. // Create-or-increment
  63. static void LocalInc()
  64. {
  65. if (local == null) { local = new ThreadLocalValue(); }
  66. ++local.ctr;
  67. }
  68.  
  69. static void Main(string[] args)
  70. {
  71. Stopwatch sw = Stopwatch.StartNew();
  72.  
  73. Parallel.For(0, 100000000, (a) =>
  74. {
  75. LocalInc();
  76. });
  77.  
  78. foreach (var item in allStorage)
  79. {
  80. ThreadLocalValue target;
  81. if (item.TryGetTarget(out target))
  82. {
  83. target.Dispose();
  84. }
  85. }
  86.  
  87. // CHANGE HERE!!!
  88. // There could be some finalizer that was already running,
  89. // we wait that they execute their Interlocked.Add
  90. GC.WaitForPendingFinalizers();
  91.  
  92. Console.WriteLine(sw.Elapsed.ToString());
  93.  
  94. Console.WriteLine(counter);
  95. Console.ReadLine();
  96. }
  97. }
  98.  
Time limit exceeded #stdin #stdout 5s 37144KB
stdin
Standard input is empty
stdout
Standard output is empty