Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to get the ids of all the currently surviving goroutines, or to judge the survival status of the current goroutines based on the id of current goroutine? #42629

Closed
sjy3 opened this issue Nov 16, 2020 · 1 comment

Comments

@sjy3
Copy link

sjy3 commented Nov 16, 2020

I created a map to store some information the current goroutine, the key is the id of the current goroutine, and I plan to delete the key-value after the goroutine dies. How can I get the status of the goroutine?

I get the goroutine Id by

func GetGoroutineId() int64 {
	var (
		buf [64]byte
		n   = runtime.Stack(buf[:], false)
		stk = strings.TrimPrefix(string(buf[:n]), "goroutine ")
	)
	idField := strings.Fields(stk)[0]
	id, err := strconv.Atoi(idField)
	if err != nil {
		log.Errorf("can not get goroutine id: %v", err)
	}

	return int64(id)
}

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

$ go version 1.15

@martisch
Copy link
Contributor

martisch commented Nov 16, 2020

See https://golang.org/doc/faq#no_goroutine_id why goroutine IDs are not supported directly.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions.

See https://github.com/golang/go/wiki/Questions for links to places to ask questions.

@golang golang locked and limited conversation to collaborators Nov 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants