golang创建linux线程,Golang 语言如何获取线程ID(Thread ID)?

Golang 性能测试,发现多核机器一直不能跑满,单核机器可以跑满,集合https://blog.csdn.net/liangzhiyang/article/details/52669851?utm_source=blogxgwz3 的调度理论,想看下某个goroutine 一秒内是否会被切走,如果被切走,就说明go在调度时会对每个核“负载均衡”。

linux 环境获取threadID的方法如下:

package main

import (

"fmt"

"golang.org/x/sys/unix"

"time"

)

func see(s string) {

//runtime.LockOSThread()

a := time.Now()

for i := 1; i < 20000; i++ {

//runtime.Gosched()

var sum int

sum = 1

sum = sum * i

fmt.Println("sum:", sum)

fmt.Println("s:", s)

getid("see " + s)

//fmt.Println("cpus:", runtime.StackRecord{})

//fmt.Println("cpus:", runtime.LockOSThread)

}

fmt.Println(time.Since(a))

}

func getid(s string) {

pid := unix.Getpid()

fmt.Println(s+" Getpid", pid)

fmt.Println(s+" Getppid", unix.Getppid())

pgid, _ := unix.Getpgid(pid)

fmt.Println(s+" Getpgid", pgid)

fmt.Println(s+" Gettid", unix.Gettid())

sid, _ := unix.Getsid(pid)

fmt.Println(s+" Getsid", sid)

fmt.Println(s+" Getegid", unix.Getegid())

fmt.Println(s+" Geteuid", unix.Geteuid())

fmt.Println(s+" Getgid", unix.Getgid())

fmt.Println(s+" Getuid", unix.Getuid())

}

func main() {

go see("hello")

go see("world")

getid("main")

time.Sleep(20 * time.Second)

}

运行结果:

main Getpid 15413

main Getppid 15361

main Getpgid 15361

main Gettid 15413

main Getsid 15333

main Getegid 1000

main Geteuid 1000

main Getgid 1000

main Getuid 1000

sum: 1

s: hello

see hello Getpid 15413

see hello Getppid 15361

see hello Getpgid 15361

see hello Gettid 15416

see hello Getsid 15333

see hello Getegid 1000

see hello Geteuid 1000

see hello Getgid 1000

see hello Getuid 1000

73.762µs

sum: 1

s: world

see world Getpid 15413

see world Getppid 15361

see world Getpgid 15361

see world Gettid 15415

see world Getsid 15333

see world Getegid 1000

see world Geteuid 1000

see world Getgid 1000

see world Getuid 1000

119.73µs

说明:window平台获取线程ID方法:

windows.GetCurrentThreadId()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值