반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 커널
- bash
- UNIX Internals
- 포인터변수
- DBMS
- TiKV
- Golang
- TiDB
- getopts
- Pointer
- kernel
- 구조와 원리
- 컴퓨터 강좌
- Windows via c/c++
- 긴옵션
- DBMS 개발
- Programming
- UNIX
- Preprocessor
- 전처리기
- Symbol
- 함수포인터
- 포인터
- 한빛미디어
- go
- FreeBSD
- OS 커널
- SQLite
- newSQL
- 약어
Archives
- Today
- Total
sonumb
go routine ID 획득하기. 본문
직접 호출 할 수 있는 함수는 제공되지 않는 듯 하다.
아래 코드를 이용하여 id를 획득하는 것으로 해결함.
간단히 설명하자면 콜스택에 ID가 기입되어 있는데, 이를 획득하여 타입변환후 반환하는 코드다.
https://gist.github.com/metafeather/3615b23097836bc36579100dac376906
Get goroutine id for debugging
Get goroutine id for debugging. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
"sync"
)
func goid() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("cannot get goroutine id: %v", err))
}
return id
}
func main() {
fmt.Println("main", goid())
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
i := i
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("[",i,"]:goid():", goid())
}()
}
wg.Wait()
}
반응형