threadpool.html 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/threadpool</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/threadpool</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li><a class="reference" id="see-also_toc" href="#see-also">See also</a></li>
  50. <li>
  51. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  52. </li>
  53. <li>
  54. <details open>
  55. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  56. <ul class="simple simple-toc-section">
  57. <li><a class="reference" href="#FlowVar" title="FlowVar[T] {.compilerproc.} = ref FlowVarObj[T]">FlowVar</a></li>
  58. <li><a class="reference" href="#FlowVarBase" title="FlowVarBase = ref FlowVarBaseObj">FlowVarBase</a></li>
  59. <li><a class="reference" href="#ThreadId" title="ThreadId = range[0 .. MaxDistinguishedThread - 1]">ThreadId</a></li>
  60. </ul>
  61. </details>
  62. </li>
  63. <li>
  64. <details open>
  65. <summary><a class="reference reference-toplevel" href="#10" id="60">Consts</a></summary>
  66. <ul class="simple simple-toc-section">
  67. <li><a class="reference" href="#MaxDistinguishedThread" title="MaxDistinguishedThread {.intdefine.} = 32">MaxDistinguishedThread</a></li>
  68. <li><a class="reference" href="#MaxThreadPoolSize" title="MaxThreadPoolSize {.intdefine.} = 256">MaxThreadPoolSize</a></li>
  69. </ul>
  70. </details>
  71. </li>
  72. <li>
  73. <details open>
  74. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  75. <ul class="simple simple-toc-section">
  76. <ul class="simple nested-toc-section">^
  77. <li><a class="reference" href="#%5E%2CFlowVar%5BT%5D" title="`^`[T](fv: FlowVar[T]): T">`^`[T](fv: FlowVar[T]): T</a></li>
  78. </ul>
  79. <ul class="simple nested-toc-section">awaitAndThen
  80. <li><a class="reference" href="#awaitAndThen%2CFlowVar%5BT%5D%2Cproc%28T%29" title="awaitAndThen[T](fv: FlowVar[T]; action: proc (x: T) {.closure.})">awaitAndThen[T](fv: FlowVar[T]; action: proc (x: T) {.closure.})</a></li>
  81. </ul>
  82. <ul class="simple nested-toc-section">blockUntil
  83. <li><a class="reference" href="#blockUntil%2CFlowVarBaseObj" title="blockUntil(fv: var FlowVarBaseObj)">blockUntil(fv: var FlowVarBaseObj)</a></li>
  84. </ul>
  85. <ul class="simple nested-toc-section">blockUntilAny
  86. <li><a class="reference" href="#blockUntilAny%2CopenArray%5BFlowVarBase%5D" title="blockUntilAny(flowVars: openArray[FlowVarBase]): int">blockUntilAny(flowVars: openArray[FlowVarBase]): int</a></li>
  87. </ul>
  88. <ul class="simple nested-toc-section">isReady
  89. <li><a class="reference" href="#isReady%2CFlowVarBase" title="isReady(fv: FlowVarBase): bool">isReady(fv: FlowVarBase): bool</a></li>
  90. </ul>
  91. <ul class="simple nested-toc-section">parallel
  92. <li><a class="reference" href="#parallel%2Cuntyped" title="parallel(body: untyped)">parallel(body: untyped)</a></li>
  93. </ul>
  94. <ul class="simple nested-toc-section">pinnedSpawn
  95. <li><a class="reference" href="#pinnedSpawn%2CThreadId%2Csinktyped" title="pinnedSpawn(id: ThreadId; call: sink typed)">pinnedSpawn(id: ThreadId; call: sink typed)</a></li>
  96. </ul>
  97. <ul class="simple nested-toc-section">preferSpawn
  98. <li><a class="reference" href="#preferSpawn" title="preferSpawn(): bool">preferSpawn(): bool</a></li>
  99. </ul>
  100. <ul class="simple nested-toc-section">setMaxPoolSize
  101. <li><a class="reference" href="#setMaxPoolSize%2Crange%5B%5D" title="setMaxPoolSize(size: range[1 .. MaxThreadPoolSize])">setMaxPoolSize(size: range[1 .. MaxThreadPoolSize])</a></li>
  102. </ul>
  103. <ul class="simple nested-toc-section">setMinPoolSize
  104. <li><a class="reference" href="#setMinPoolSize%2Crange%5B%5D" title="setMinPoolSize(size: range[1 .. MaxThreadPoolSize])">setMinPoolSize(size: range[1 .. MaxThreadPoolSize])</a></li>
  105. </ul>
  106. <ul class="simple nested-toc-section">spawn
  107. <li><a class="reference" href="#spawn%2Csinktyped" title="spawn(call: sink typed)">spawn(call: sink typed)</a></li>
  108. </ul>
  109. <ul class="simple nested-toc-section">sync
  110. <li><a class="reference" href="#sync" title="sync()">sync()</a></li>
  111. </ul>
  112. <ul class="simple nested-toc-section">unsafeRead
  113. <li><a class="reference" href="#unsafeRead%2CFlowVar%5Bref.T%5D" title="unsafeRead[T](fv: FlowVar[ref T]): ptr T">unsafeRead[T](fv: FlowVar[ref T]): ptr T</a></li>
  114. </ul>
  115. </ul>
  116. </details>
  117. </li>
  118. <li>
  119. <details open>
  120. <summary><a class="reference reference-toplevel" href="#18" id="68">Templates</a></summary>
  121. <ul class="simple simple-toc-section">
  122. <ul class="simple nested-toc-section">spawnX
  123. <li><a class="reference" href="#spawnX.t" title="spawnX(call)">spawnX(call)</a></li>
  124. </ul>
  125. </ul>
  126. </details>
  127. </li>
  128. </ul>
  129. </div>
  130. <div class="nine columns" id="content">
  131. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  132. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  133. <div id="tocRoot"></div>
  134. <div class="deprecation-message">
  135. <b>Deprecated:</b> use the nimble packages `malebolgia`, `taskpools` or `weave` instead
  136. </div>
  137. <p class="module-desc"><p>Implements Nim's <a class="reference external" href="manual_experimental.html#parallel-amp-spawn">parallel &amp; spawn statements</a>.</p>
  138. <p>Unstable API.</p>
  139. <h1><a class="toc-backref" id="see-also" href="#see-also">See also</a></h1><ul class="simple"><li><a class="reference external" href="typedthreads.html">threads module</a> for basic thread support</li>
  140. <li><a class="reference external" href="locks.html">locks module</a> for locks and condition variables</li>
  141. <li><a class="reference external" href="asyncdispatch.html">asyncdispatch module</a> for asynchronous IO</li>
  142. </ul>
  143. </p>
  144. <div class="section" id="6">
  145. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  146. <dl class="item">
  147. <a class="reference external" href="cpuinfo.html">cpuinfo</a>, <a class="reference external" href="cpuload.html">cpuload</a>, <a class="reference external" href="locks.html">locks</a>, <a class="reference external" href="os.html">os</a>, <a class="reference external" href="assertions.html">assertions</a>, <a class="reference external" href="typedthreads.html">typedthreads</a>, <a class="reference external" href="sysatomics.html">sysatomics</a>
  148. </dl>
  149. </div>
  150. <div class="section" id="7">
  151. <h1><a class="toc-backref" href="#7">Types</a></h1>
  152. <dl class="item">
  153. <div id="FlowVar">
  154. <dt><pre><a href="threadpool.html#FlowVar"><span class="Identifier">FlowVar</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> {.<span class="Identifier">compilerproc</span>.} <span class="Other">=</span> <span class="Keyword">ref</span> <span class="Identifier">FlowVarObj</span><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  155. <dd>
  156. A data flow variable.
  157. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L119" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  158. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L119" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  159. </dd>
  160. </div>
  161. <div id="FlowVarBase">
  162. <dt><pre><a href="threadpool.html#FlowVarBase"><span class="Identifier">FlowVarBase</span></a> <span class="Other">=</span> <span class="Keyword">ref</span> <span class="Identifier">FlowVarBaseObj</span></pre></dt>
  163. <dd>
  164. Untyped base class for <a class="reference external" href="#FlowVar">FlowVar[T]</a>.
  165. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L106" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  166. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L106" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  167. </dd>
  168. </div>
  169. <div id="ThreadId">
  170. <dt><pre><a href="threadpool.html#ThreadId"><span class="Identifier">ThreadId</span></a> <span class="Other">=</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">0</span> <span class="Operator">..</span> <a href="threadpool.html#MaxDistinguishedThread"><span class="Identifier">MaxDistinguishedThread</span></a> <span class="Operator">-</span> <span class="DecNumber">1</span><span class="Other">]</span></pre></dt>
  171. <dd>
  172. A thread identifier.
  173. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L322" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  174. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L322" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  175. </dd>
  176. </div>
  177. </dl>
  178. </div>
  179. <div class="section" id="10">
  180. <h1><a class="toc-backref" href="#10">Consts</a></h1>
  181. <dl class="item">
  182. <div id="MaxDistinguishedThread">
  183. <dt><pre><a href="threadpool.html#MaxDistinguishedThread"><span class="Identifier">MaxDistinguishedThread</span></a> {.<span class="Identifier">intdefine</span>.} <span class="Other">=</span> <span class="DecNumber">32</span></pre></dt>
  184. <dd>
  185. Maximum number of &quot;distinguished&quot; threads.
  186. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L319" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  187. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L319" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  188. </dd>
  189. </div>
  190. <div id="MaxThreadPoolSize">
  191. <dt><pre><a href="threadpool.html#MaxThreadPoolSize"><span class="Identifier">MaxThreadPoolSize</span></a> {.<span class="Identifier">intdefine</span>.} <span class="Other">=</span> <span class="DecNumber">256</span></pre></dt>
  192. <dd>
  193. Maximum size of the thread pool. 256 threads should be good enough for anybody ;-)
  194. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L317" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  195. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L317" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  196. </dd>
  197. </div>
  198. </dl>
  199. </div>
  200. <div class="section" id="12">
  201. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  202. <dl class="item">
  203. <div id="^-procs-all">
  204. <div id="^,FlowVar[T]">
  205. <dt><pre><span class="Keyword">proc</span> <a href="#%5E%2CFlowVar%5BT%5D"><span class="Identifier">`^`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">fv</span><span class="Other">:</span> <a href="threadpool.html#FlowVar"><span class="Identifier">FlowVar</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  206. <dd>
  207. Blocks until the value is available and then returns this value.
  208. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L263" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  209. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L263" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  210. </dd>
  211. </div>
  212. </div>
  213. <div id="awaitAndThen-procs-all">
  214. <div id="awaitAndThen,FlowVar[T],proc(T)">
  215. <dt><pre><span class="Keyword">proc</span> <a href="#awaitAndThen%2CFlowVar%5BT%5D%2Cproc%28T%29"><span class="Identifier">awaitAndThen</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">fv</span><span class="Other">:</span> <a href="threadpool.html#FlowVar"><span class="Identifier">FlowVar</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">action</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">closure</span>.}<span class="Other">)</span></pre></dt>
  216. <dd>
  217. <p>Blocks until <tt class="docutils literal"><span class="pre"><span class="Identifier">fv</span></span></tt> is available and then passes its value to <tt class="docutils literal"><span class="pre"><span class="Identifier">action</span></span></tt>.</p>
  218. <p>Note that due to Nim's parameter passing semantics, this means that <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> doesn't need to be copied, so <tt class="docutils literal"><span class="pre"><span class="Identifier">awaitAndThen</span></span></tt> can sometimes be more efficient than the <a class="reference external" href="#^,FlowVar[T]">^ proc</a>.</p>
  219. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L236" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  220. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L236" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  221. </dd>
  222. </div>
  223. </div>
  224. <div id="blockUntil-procs-all">
  225. <div id="blockUntil,FlowVarBaseObj">
  226. <dt><pre><span class="Keyword">proc</span> <a href="#blockUntil%2CFlowVarBaseObj"><span class="Identifier">blockUntil</span></a><span class="Other">(</span><span class="Identifier">fv</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">FlowVarBaseObj</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  227. <dd>
  228. <p>Waits until the value for <tt class="docutils literal"><span class="pre"><span class="Identifier">fv</span></span></tt> arrives.</p>
  229. <p>Usually it is not necessary to call this explicitly.</p>
  230. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L142" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  231. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L142" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  232. </dd>
  233. </div>
  234. </div>
  235. <div id="blockUntilAny-procs-all">
  236. <div id="blockUntilAny,openArray[FlowVarBase]">
  237. <dt><pre><span class="Keyword">proc</span> <a href="#blockUntilAny%2CopenArray%5BFlowVarBase%5D"><span class="Identifier">blockUntilAny</span></a><span class="Other">(</span><span class="Identifier">flowVars</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><a href="threadpool.html#FlowVarBase"><span class="Identifier">FlowVarBase</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  238. <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  239. <dd>
  240. <p>Awaits any of the given <tt class="docutils literal"><span class="pre"><span class="Identifier">flowVars</span></span></tt>. Returns the index of one <tt class="docutils literal"><span class="pre"><span class="Identifier">flowVar</span></span></tt> for which a value arrived.</p>
  241. <p>A <tt class="docutils literal"><span class="pre"><span class="Identifier">flowVar</span></span></tt> only supports one call to <tt class="docutils literal"><span class="pre"><span class="Identifier">blockUntilAny</span></span></tt> at the same time. That means if you <tt class="docutils literal"><span class="pre"><span class="Identifier">blockUntilAny</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="Identifier">a</span><span class="Punctuation">,</span><span class="Identifier">b</span><span class="Punctuation">]</span><span class="Punctuation">)</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">blockUntilAny</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="Identifier">b</span><span class="Punctuation">,</span><span class="Identifier">c</span><span class="Punctuation">]</span><span class="Punctuation">)</span></span></tt> the second call will only block until <tt class="docutils literal"><span class="pre"><span class="Identifier">c</span></span></tt>. If there is no <tt class="docutils literal"><span class="pre"><span class="Identifier">flowVar</span></span></tt> left to be able to wait on, -1 is returned.</p>
  242. <p><strong>Note:</strong> This results in non-deterministic behaviour and should be avoided.</p>
  243. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L272" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  244. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L272" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  245. </dd>
  246. </div>
  247. </div>
  248. <div id="isReady-procs-all">
  249. <div id="isReady,FlowVarBase">
  250. <dt><pre><span class="Keyword">proc</span> <a href="#isReady%2CFlowVarBase"><span class="Identifier">isReady</span></a><span class="Other">(</span><span class="Identifier">fv</span><span class="Other">:</span> <a href="threadpool.html#FlowVarBase"><span class="Identifier">FlowVarBase</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  251. <dd>
  252. <p>Determines whether the specified <tt class="docutils literal"><span class="pre"><span class="Identifier">FlowVarBase</span></span></tt>'s value is available.</p>
  253. <p>If <tt class="docutils literal"><span class="pre"><span class="Identifier">true</span></span></tt>, awaiting <tt class="docutils literal"><span class="pre"><span class="Identifier">fv</span></span></tt> will not block.</p>
  254. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L301" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  255. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L301" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  256. </dd>
  257. </div>
  258. </div>
  259. <div id="parallel-procs-all">
  260. <div id="parallel,untyped">
  261. <dt><pre><span class="Keyword">proc</span> <a href="#parallel%2Cuntyped"><span class="Identifier">parallel</span></a><span class="Other">(</span><span class="Identifier">body</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span> {.<span class="Identifier">magic</span><span class="Other">:</span> <span class="StringLit">&quot;Parallel&quot;</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  262. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  263. <dd>
  264. <p>A parallel section can be used to execute a block in parallel.</p>
  265. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">body</span></span></tt> has to be in a DSL that is a particular subset of the language.</p>
  266. <p>Please refer to <a class="reference external" href="manual_experimental.html#parallel-amp-spawn">the manual</a> for further information.</p>
  267. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L484" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  268. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L484" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  269. </dd>
  270. </div>
  271. </div>
  272. <div id="pinnedSpawn-procs-all">
  273. <div id="pinnedSpawn,ThreadId,sinktyped">
  274. <dt><pre><span class="Keyword">proc</span> <a href="#pinnedSpawn%2CThreadId%2Csinktyped"><span class="Identifier">pinnedSpawn</span></a><span class="Other">(</span><span class="Identifier">id</span><span class="Other">:</span> <a href="threadpool.html#ThreadId"><span class="Identifier">ThreadId</span></a><span class="Other">;</span> <span class="Identifier">call</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">)</span> {.<span class="Identifier">magic</span><span class="Other">:</span> <span class="StringLit">&quot;Spawn&quot;</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  275. <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  276. <dd>
  277. <p>Always spawns a new task on the worker thread with <tt class="docutils literal"><span class="pre"><span class="Identifier">id</span></span></tt>, so that the <tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> is <strong>always</strong> executed on the thread.</p>
  278. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> has to be a proc call <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Punctuation">(</span><span class="Operator">...</span><span class="Punctuation">)</span></span></tt> where <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span></span></tt> is gcsafe and has a return type that is either <tt class="docutils literal"><span class="pre"><span class="Identifier">void</span></span></tt> or compatible with <tt class="docutils literal"><span class="pre"><span class="Identifier">FlowVar</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span></span></tt>.</p>
  279. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L465" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  280. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L465" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  281. </dd>
  282. </div>
  283. </div>
  284. <div id="preferSpawn-procs-all">
  285. <div id="preferSpawn">
  286. <dt><pre><span class="Keyword">proc</span> <a href="#preferSpawn"><span class="Identifier">preferSpawn</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  287. <dd>
  288. <p>Use this proc to determine quickly if a <tt class="docutils literal"><span class="pre"><span class="Identifier">spawn</span></span></tt> or a direct call is preferable.</p>
  289. <p>If it returns <tt class="docutils literal"><span class="pre"><span class="Identifier">true</span></span></tt>, a <tt class="docutils literal"><span class="pre"><span class="Identifier">spawn</span></span></tt> may make sense. In general it is not necessary to call this directly; use the <a class="reference external" href="#spawnX.t">spawnX template</a> instead.</p>
  290. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L448" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  291. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L448" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  292. </dd>
  293. </div>
  294. </div>
  295. <div id="setMaxPoolSize-procs-all">
  296. <div id="setMaxPoolSize,range[]">
  297. <dt><pre><span class="Keyword">proc</span> <a href="#setMaxPoolSize%2Crange%5B%5D"><span class="Identifier">setMaxPoolSize</span></a><span class="Other">(</span><span class="Identifier">size</span><span class="Other">:</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="Identifier">MaxThreadPoolSize</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  298. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  299. <dd>
  300. Sets the maximum thread pool size. The default value of this is <a class="reference external" href="#MaxThreadPoolSize">MaxThreadPoolSize</a>.
  301. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L407" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  302. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L407" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  303. </dd>
  304. </div>
  305. </div>
  306. <div id="setMinPoolSize-procs-all">
  307. <div id="setMinPoolSize,range[]">
  308. <dt><pre><span class="Keyword">proc</span> <a href="#setMinPoolSize%2Crange%5B%5D"><span class="Identifier">setMinPoolSize</span></a><span class="Other">(</span><span class="Identifier">size</span><span class="Other">:</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="Identifier">MaxThreadPoolSize</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  309. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  310. <dd>
  311. Sets the minimum thread pool size. The default value of this is 4.
  312. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L403" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  313. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L403" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  314. </dd>
  315. </div>
  316. </div>
  317. <div id="spawn-procs-all">
  318. <div id="spawn,sinktyped">
  319. <dt><pre><span class="Keyword">proc</span> <a href="#spawn%2Csinktyped"><span class="Identifier">spawn</span></a><span class="Other">(</span><span class="Identifier">call</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">)</span> {.<span class="Identifier">magic</span><span class="Other">:</span> <span class="StringLit">&quot;Spawn&quot;</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  320. <dd>
  321. <p>Always spawns a new task, so that the <tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> is never executed on the calling thread.</p>
  322. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> has to be a proc call <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Punctuation">(</span><span class="Operator">...</span><span class="Punctuation">)</span></span></tt> where <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span></span></tt> is gcsafe and has a return type that is either <tt class="docutils literal"><span class="pre"><span class="Identifier">void</span></span></tt> or compatible with <tt class="docutils literal"><span class="pre"><span class="Identifier">FlowVar</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span></span></tt>.</p>
  323. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L457" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  324. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L457" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  325. </dd>
  326. </div>
  327. </div>
  328. <div id="sync-procs-all">
  329. <div id="sync">
  330. <dt><pre><span class="Keyword">proc</span> <a href="#sync"><span class="Identifier">sync</span></a><span class="Other">(</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">TimeEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  331. <dd>
  332. <p>A simple barrier to wait for all <tt class="docutils literal"><span class="pre"><span class="Identifier">spawn</span></span></tt>ed tasks.</p>
  333. <p>If you need more elaborate waiting, you have to use an explicit barrier.</p>
  334. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L593" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  335. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L593" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  336. </dd>
  337. </div>
  338. </div>
  339. <div id="unsafeRead-procs-all">
  340. <div id="unsafeRead,FlowVar[ref.T]">
  341. <dt><pre><span class="Keyword">proc</span> <a href="#unsafeRead%2CFlowVar%5Bref.T%5D"><span class="Identifier">unsafeRead</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">fv</span><span class="Other">:</span> <a href="threadpool.html#FlowVar"><span class="Identifier">FlowVar</span></a><span class="Other">[</span><span class="Keyword">ref</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Keyword">ptr</span> <span class="Identifier">T</span></pre></dt>
  342. <dd>
  343. Blocks until the value is available and then returns this value.
  344. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L254" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  345. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L254" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  346. </dd>
  347. </div>
  348. </div>
  349. </dl>
  350. </div>
  351. <div class="section" id="18">
  352. <h1><a class="toc-backref" href="#18">Templates</a></h1>
  353. <dl class="item">
  354. <div id="spawnX-templates-all">
  355. <div id="spawnX.t">
  356. <dt><pre><span class="Keyword">template</span> <a href="#spawnX.t"><span class="Identifier">spawnX</span></a><span class="Other">(</span><span class="Identifier">call</span><span class="Other">)</span></pre></dt>
  357. <dd>
  358. <p>Spawns a new task if a CPU core is ready, otherwise executes the call in the calling thread.</p>
  359. <p>Usually, it is advised to use the <a class="reference external" href="#spawn,sinktyped">spawn proc</a> in order to not block the producer for an unknown amount of time.</p>
  360. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> has to be a proc call <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Punctuation">(</span><span class="Operator">...</span><span class="Punctuation">)</span></span></tt> where <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span></span></tt> is gcsafe and has a return type that is either 'void' or compatible with <tt class="docutils literal"><span class="pre"><span class="Identifier">FlowVar</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span></span></tt>.</p>
  361. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/concurrency/threadpool.nim#L473" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  362. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/concurrency/threadpool.nim#L473" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  363. </dd>
  364. </div>
  365. </div>
  366. </dl>
  367. </div>
  368. </div>
  369. </div>
  370. <div class="twelve-columns footer">
  371. <span class="nim-sprite"></span>
  372. <br>
  373. <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-15 14:13:35 UTC</small>
  374. </div>
  375. </div>
  376. </div>
  377. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  378. </body>
  379. </html>