thpool.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**********************************
  2. * @author Johan Hanssen Seferidis
  3. * License: MIT
  4. *
  5. **********************************/
  6. #ifndef _THPOOL_
  7. #define _THPOOL_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* =================================== API ======================================= */
  12. typedef struct thpool_* threadpool;
  13. /**
  14. * @brief Initialize threadpool
  15. *
  16. * Initializes a threadpool. This function will not return until all
  17. * threads have initialized successfully.
  18. *
  19. * @example
  20. *
  21. * ..
  22. * threadpool thpool; //First we declare a threadpool
  23. * thpool = thpool_init(4); //then we initialize it to 4 threads
  24. * ..
  25. *
  26. * @param num_threads number of threads to be created in the threadpool
  27. * @return threadpool created threadpool on success,
  28. * NULL on error
  29. */
  30. threadpool thpool_init(int num_threads);
  31. /**
  32. * @brief Add work to the job queue
  33. *
  34. * Takes an action and its argument and adds it to the threadpool's job queue.
  35. * If you want to add to work a function with more than one arguments then
  36. * a way to implement this is by passing a pointer to a structure.
  37. *
  38. * NOTICE: You have to cast both the function and argument to not get warnings.
  39. *
  40. * @example
  41. *
  42. * void print_num(int num){
  43. * printf("%d\n", num);
  44. * }
  45. *
  46. * int main() {
  47. * ..
  48. * int a = 10;
  49. * thpool_add_work(thpool, (void*)print_num, (void*)a);
  50. * ..
  51. * }
  52. *
  53. * @param threadpool threadpool to which the work will be added
  54. * @param function_p pointer to function to add as work
  55. * @param arg_p pointer to an argument
  56. * @return 0 on success, -1 otherwise.
  57. */
  58. int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
  59. /**
  60. * @brief Wait for all queued jobs to finish
  61. *
  62. * Will wait for all jobs - both queued and currently running to finish.
  63. * Once the queue is empty and all work has completed, the calling thread
  64. * (probably the main program) will continue.
  65. *
  66. * Smart polling is used in wait. The polling is initially 0 - meaning that
  67. * there is virtually no polling at all. If after 1 seconds the threads
  68. * haven't finished, the polling interval starts growing exponentially
  69. * until it reaches max_secs seconds. Then it jumps down to a maximum polling
  70. * interval assuming that heavy processing is being used in the threadpool.
  71. *
  72. * @example
  73. *
  74. * ..
  75. * threadpool thpool = thpool_init(4);
  76. * ..
  77. * // Add a bunch of work
  78. * ..
  79. * thpool_wait(thpool);
  80. * puts("All added work has finished");
  81. * ..
  82. *
  83. * @param threadpool the threadpool to wait for
  84. * @return nothing
  85. */
  86. void thpool_wait(threadpool);
  87. /**
  88. * @brief Pauses all threads immediately
  89. *
  90. * The threads will be paused no matter if they are idle or working.
  91. * The threads return to their previous states once thpool_resume
  92. * is called.
  93. *
  94. * While the thread is being paused, new work can be added.
  95. *
  96. * @example
  97. *
  98. * threadpool thpool = thpool_init(4);
  99. * thpool_pause(thpool);
  100. * ..
  101. * // Add a bunch of work
  102. * ..
  103. * thpool_resume(thpool); // Let the threads start their magic
  104. *
  105. * @param threadpool the threadpool where the threads should be paused
  106. * @return nothing
  107. */
  108. void thpool_pause(threadpool);
  109. /**
  110. * @brief Unpauses all threads if they are paused
  111. *
  112. * @example
  113. * ..
  114. * thpool_pause(thpool);
  115. * sleep(10); // Delay execution 10 seconds
  116. * thpool_resume(thpool);
  117. * ..
  118. *
  119. * @param threadpool the threadpool where the threads should be unpaused
  120. * @return nothing
  121. */
  122. void thpool_resume(threadpool);
  123. /**
  124. * @brief Destroy the threadpool
  125. *
  126. * This will wait for the currently active threads to finish and then 'kill'
  127. * the whole threadpool to free up memory.
  128. *
  129. * @example
  130. * int main() {
  131. * threadpool thpool1 = thpool_init(2);
  132. * threadpool thpool2 = thpool_init(2);
  133. * ..
  134. * thpool_destroy(thpool1);
  135. * ..
  136. * return 0;
  137. * }
  138. *
  139. * @param threadpool the threadpool to destroy
  140. * @return nothing
  141. */
  142. void thpool_destroy(threadpool);
  143. /**
  144. * @brief Show currently working threads
  145. *
  146. * Working threads are the threads that are performing work (not idle).
  147. *
  148. * @example
  149. * int main() {
  150. * threadpool thpool1 = thpool_init(2);
  151. * threadpool thpool2 = thpool_init(2);
  152. * ..
  153. * printf("Working threads: %d\n", thpool_num_threads_working(thpool1));
  154. * ..
  155. * return 0;
  156. * }
  157. *
  158. * @param threadpool the threadpool of interest
  159. * @return integer number of threads working
  160. */
  161. int thpool_num_threads_working(threadpool);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif