Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Update (Go version 1.2+)</h2> <p>As of Go 1.2, the scheduler works on the principle of <strong>pre-emptive multitasking</strong>. This means that the problem in the original question (and the solution presented below) are no longer relevant.</p> <p>From the <a href="http://golang.org/doc/go1.2#preemption">Go 1.2 release notes</a></p> <blockquote> <p>Pre-emption in the scheduler</p> <p>In prior releases, a goroutine that was looping forever could starve out other goroutines on the same thread, a serious problem when GOMAXPROCS provided only one user thread. In Go > 1.2, this is partially addressed: The scheduler is invoked occasionally upon entry to a function. This means that any loop that includes a (non-inlined) function call can be pre-empted, allowing other goroutines to run on the same thread.</p> </blockquote> <h2>Short answer</h2> <p>It is not blocking on the writes. It is stuck in the infinite loop of <code>processevents</code>. This loop never yields to the scheduler, causing all goroutines to lock indefinitely.</p> <p>If you comment out the call to <code>processevents</code>, you will get results as expected, right until the 100th write. At which point the program panics, because nobody reads from the channel.</p> <p>Another solution is to put a call to <code>runtime.Gosched()</code> in the loop.</p> <h2>Long answer</h2> <p>With Go1.0.2, Go's scheduler works on the principle of <a href="http://en.wikipedia.org/wiki/Cooperative_multitasking">Cooperative multitasking</a>. This means that it allocates CPU time to the various goroutines running within a given OS thread by having these routines interact with the scheduler in certain conditions. These 'interactions' occur when certain types of code are executed in a goroutine. In go's case this involves doing some kind of I/O, syscalls or memory allocation (in certain conditions).</p> <p>In the case of an empty loop, no such conditions are ever encountered. The scheduler is therefore never allowed to run its scheduling algorithms for as long as that loop is running. This consequently prevents it from allotting CPU time to other goroutines waiting to be run and the result you observed ensues: You effectively created a deadlock that can not be detected or broken out of by the scheduler.</p> <p>The empty loop is usually never desired in Go and will, in most cases, indicate a bug in the program. If you do need it for whatever reason, you have to manually yield to the scheduler by calling <code>runtime.Gosched()</code> in every iteration.</p> <pre><code>for { runtime.Gosched() } </code></pre> <p>Setting <code>GOMAXPROCS</code> to a value <code>&gt; 1</code> was mentioned as a solution. While this will get rid of the immediate problem you observed, it will effectively move the problem to a different OS thread, if the scheduler decides to move the looping goroutine to its own OS thread that is. There is no guarantee of this, unless you call <code>runtime.LockOSThread()</code> at the start of the <code>processevents</code> function. Even then, I would still not rely on this approach to be a good solution. Simply calling <code>runtime.Gosched()</code> in the loop itself, will solve all the issues, regardless of which OS thread the goroutine is running in.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload