Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:21:50+00:00 2026-06-11T09:21:50+00:00

I wrote the following program: package main import ( fmt ) func processevents(list chan

  • 0

I wrote the following program:

package main

import (
    "fmt"
)

func processevents(list chan func()) {
    for {
        //a := <-list
        //a()
    }
}

func test() {
    fmt.Println("Ho!")
}

func main() {

    eventlist := make(chan func(), 100)

    go processevents(eventlist)

    for {
        eventlist <- test
        fmt.Println("Hey!")
    }
}

Since the channel eventlist is a buffered channel, I think I should get at exactly 100 times the output “Hey!”, but it is displayed only once. Where is my mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:21:51+00:00Added an answer on June 11, 2026 at 9:21 am

    Update (Go version 1.2+)

    As of Go 1.2, the scheduler works on the principle of pre-emptive multitasking.
    This means that the problem in the original question (and the solution presented below) are no longer relevant.

    From the Go 1.2 release notes

    Pre-emption in the scheduler

    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.

    Short answer

    It is not blocking on the writes. It is stuck in the infinite loop of processevents.
    This loop never yields to the scheduler, causing all goroutines to lock indefinitely.

    If you comment out the call to processevents, you will get results as expected, right until the 100th write. At which point the program panics, because nobody reads from the channel.

    Another solution is to put a call to runtime.Gosched() in the loop.

    Long answer

    With Go1.0.2, Go’s scheduler works on the principle of Cooperative multitasking.
    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).

    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.

    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 runtime.Gosched() in every iteration.

    for {
        runtime.Gosched()
    }
    

    Setting GOMAXPROCS to a value > 1 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 runtime.LockOSThread() at the start of the processevents function. Even then, I would still not rely on this approach to be a good solution. Simply calling runtime.Gosched() in the loop itself, will solve all the issues, regardless of which OS thread the goroutine is running in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote following program #include<stdio.h> main () { extern int i; printf(\n%d,i); } int
I wrote following program int main () { char a=0xf; a=a+1; printf(%c\n,a); } the
I wrote the following program: #include<stdio.h> int main(void) { float f; printf(\nInput a floating-point
i wrote the following program #include <stdio.h> main() { int i = 2; float
I wrote the following program using VS2008: #include <fstream> int main() { std::wofstream fout(myfile);
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
In Visual C++ i wrote the following sample in a C++ program: float f1
I'm learning to work with lucene. I wrote a simple program to test lucene
I am a beginner, and wrote the following program for fun, to search through
I wrote the following example program but it crashes with segfault. The problem seems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.