Skip to content

An empty for{} will block large slice allocation in another goroutine, even with GOMAXPROCS > 1 ? #17174

Closed
@yaofu1986

Description

@yaofu1986

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.6.1 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/yaofu/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

package main

import (
    "runtime"
)

func work() {
    println("before")
    s := make([]int, 6000000)
    println("after")
    s[0] = 1
}

func main() {
    runtime.GOMAXPROCS(4)

    //work()   // This is OK. Very quick.
    go work()

    for {
    }
}

PS: I know for{} busy wait is no good. But I'm curious to know the underlying reason.

What did you expect to see?

following print:

before
after

What did you see instead?

following printed:

before

Activity

randall77

randall77 commented on Sep 21, 2016

@randall77
Contributor

Go has a cooperative, not preemptive, scheduler. The for{} loop never reaches a preemption point, so a garbage collection cannot complete.
Dup of #10958

ianlancetaylor

ianlancetaylor commented on Sep 21, 2016

@ianlancetaylor
Contributor

If you want to block forever, use select {}.

quentinmit

quentinmit commented on Sep 21, 2016

@quentinmit
Contributor

Can/should the compiler automatically turn for {} into select {}?

On Sep 21, 2016 09:11, "Ian Lance Taylor" notifications@github.com wrote:

If you want to block forever, use select {}.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#17174 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAHEMWI0mZF8FIy7xSwyfhN07SZLFkxNks5qsS0BgaJpZM4KCasB
.

ianlancetaylor

ianlancetaylor commented on Sep 21, 2016

@ianlancetaylor
Contributor

It could but I don't think it's worth it. Nobody writes for {} in real code.

minux

minux commented on Sep 22, 2016

@minux
Member
locked and limited conversation to collaborators on Sep 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @quentinmit@minux@ianlancetaylor@randall77@gopherbot

        Issue actions

          An empty for{} will block large slice allocation in another goroutine, even with GOMAXPROCS > 1 ? · Issue #17174 · golang/go