Thread Pooling in NETMF by EvoMotors

Jun. 13, 2016   |   Snippet   |   Licensed as Apache 2.0   |   1249 views

Thread pooling in NET Micro Framework.

Comments or questions?   Discuss on the forum.



Author Version Date
EvoMotors 1 06/13 '16 at 11:07am
1 — Source
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.Threading;
  5.  
  6. public static class ThreadUtil
  7. {
  8. /// <summary>
  9. /// Synchronizes thread queue actions
  10. /// </summary>
  11. private static readonly object lockObject = new object();
  12.  
  13. /// <summary>
  14. /// List storing our available threadpool threads
  15. /// </summary>
  16. private static readonly ArrayList availableThreads = new ArrayList();
  17.  
  18. /// <summary>
  19. /// Queue of actions for our threadpool
  20. /// </summary>
  21. private static readonly Queue threadActions = new Queue();
  22.  
  23. /// <summary>
  24. /// Wait handle for us to synchronize de-queuing thread actions
  25. /// </summary>
  26. private static readonly ManualResetEvent threadSynch = new ManualResetEvent(false);
  27.  
  28. /// <summary>
  29. /// Maximum size of our thread pool
  30. /// </summary>
  31. public static int MaximumThreads { get; set; }
  32.  
  33. /// <summary>
  34. /// Starts a new thread with an action
  35. /// </summary>
  36. /// <param name="start"></param>
  37. public static void Start(ThreadStart start)
  38. {
  39. try
  40. {
  41. var t = new Thread(start);
  42. t.Start();
  43. }
  44. catch (Exception ex)
  45. {
  46. Debug.Print(ex.ToString());
  47. }
  48. }
  49.  
  50.  
  51. /// <summary>
  52. /// Queues an action into the threadpool
  53. /// </summary>
  54. /// <param name="start"></param>
  55. public static void Queue(ThreadStart start)
  56. {
  57. if (MaximumThreads == 0)
  58. MaximumThreads = 1;
  59.  
  60. lock (lockObject)
  61. {
  62. threadActions.Enqueue(start);
  63.  
  64. // if we haven't spun all the threads up, create a new one
  65. // and add it to our available threads
  66. if (availableThreads.Count < MaximumThreads)
  67. {
  68. var t = new Thread(ActionConsumer);
  69. availableThreads.Add(t);
  70. t.Start();
  71. }
  72.  
  73. // pulse all waiting threads
  74. threadSynch.Set();
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// Indefinitely wait until an action is queued. When an action is de-queued safely execute it
  80. /// </summary>
  81. private static void ActionConsumer()
  82. {
  83. while (true)
  84. {
  85.             // wait on action pulse
  86.             threadSynch.WaitOne();
  87.  
  88. ThreadStart action = null;
  89.  
  90. // try and de-queue an action
  91. lock (lockObject)
  92. {
  93. if (threadActions.Count > 0)
  94. action = threadActions.Dequeue() as ThreadStart;
  95. else
  96. // Safely reset the mutex so that everyone waits until the next action is queued
  97. threadSynch.Reset();
  98. }
  99.  
  100. // if we got an action execute it
  101. if (action != null)
  102. {
  103. try
  104. {
  105. action();
  106. }
  107. catch (Exception ex)
  108. {
  109. Debug.Print("Error in thread pool: " + ex);
  110. }
  111. }
  112. }
  113. }
  114. }