"Pin" goroutine to core

blov · · 644 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I was wondering if there is a way to &#34;pin&#34; goroutines to cores in go. An example scenario would be: I want to run X goroutines, where X is the number of cores and make sure that each goroutine is always scheduled on the same core. Maybe the scheduler already does that when X = num CPUs, I&#39;m not sure?</p> <hr/>**评论:**<br/><br/>thokk: <pre><p><a href="https://golang.org/pkg/runtime/#LockOSThread">https://golang.org/pkg/runtime/#LockOSThread</a></p></pre>Feribg: <pre><p>Thanks, do you happen to know if that&#39;s a default behavior if num of goroutines = num of worker threads?</p></pre>captncraig: <pre><p>It is not. If your goroutine blocks, nothing else will be scheduled on that OS thread. This will lead to new os threads being spawned, and will really hurt your performance if used improperly.</p> <p>You really should not concern yourself with such details unless you have a really good reason to. </p></pre>Feribg: <pre><p>Why would new threads be created, I though the go runtime always allocates numcpu worker threads? So even if i block in the locked thread, it should just keep a static number of worker threads, right? My understanding is that this api call will basically convert a go routine to essentially a plain thread.</p></pre>captncraig: <pre><p>&#34;Until the calling goroutine exits or calls UnlockOSThread, it will always execute in that thread, and no other goroutine can&#34;.</p> <p>LockOSThread essentially takes the M out of the scheduler&#39;s pool of available os threads that it uses when it needs to run a G.</p> <p>&#34;The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit. This package&#39;s GOMAXPROCS function queries and changes the limit. &#34;</p> <p>That is not clear if locked goroutines count against this number, but it seems logical that they would not. If you have GOMAXPROCS=2 and 5 goroutines with locked os threads, you would have deadlock. New threads need to be spawned. </p> <p>I&#39;m gonna test now to confirm.</p></pre>captncraig: <pre><p>Confirmed with</p> <pre><code>package main import ( &#34;fmt&#34; &#34;runtime&#34; &#34;time&#34; ) func main() { for { time.Sleep(time.Second) go func() { runtime.LockOSThread() select {} }() } select {} } </code></pre> <p>and this incantation: </p> <pre><code>NUM=`ps M &lt;pid&gt; | wc -l | xargs` &amp;&amp; expr $NUM - 1 </code></pre> <p>that number of threads does increase as more threads get locked.</p></pre>pierrrre: <pre><p>Don&#39;t do that.</p></pre>Feribg: <pre><p>My question is how to do it not should i or not :)</p></pre>Niriel: <pre><p>OpenGL does not work properly if called from different threads, it is mandatory to reserve one goroutine for opengl and lock it to a thread.</p></pre>boomshroom: <pre><p>That&#39;s why Vulkan is happening.</p></pre>pierrrre: <pre><p>He didn&#39;t say anything about OpenGL.</p></pre>Niriel: <pre><p>I know. Poster said &#34;don&#39;t do that&#34;; I gave one example to show that, sometimes, locking a goroutine to one thread is unavoidable. In these (hopefully rare) cases, we should be able to give some constructive advice.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

644 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传