LoginSignup
0
0

More than 3 years have passed since last update.

golang bulitinのprintとruntime

Posted at

golangでCPU数やgoroutine数を知るためのruntimeを流してみた。ついでに、builtinのprintを使ってみた

go routineを呼んで、その数が変わることを確かめた。

test_runtime.go
package main                                                                    

import (
    "runtime"
    "time"
)

func RuntimeSurvey() {
    print("NumCPU:", runtime.NumCPU(), "\n")
    print("NumGoroutine:", runtime.NumGoroutine(), "\n")
    print("Version:", runtime.Version(), "\n")
    print("OS:", runtime.GOOS, "\n")
}

func main() {
    go func() {
        time.Sleep(time.Second * 1)
    }() 
    print("Before sleep")
    RuntimeSurvey()

    time.Sleep(time.Second * 1)
    print("After sleep")
    RuntimeSurvey()
}

結果はこういうふうになります。goroutineの数が、2になって、そのあと1になってます。
処理が終わって待機中みたいになったら、数に含まれなくなるのかな。

$ go run test_runtime
Before sleepNumCPU:8
NumGoroutine:2
Version:go1.14.2
OS:darwin
After sleepNumCPU:8
NumGoroutine:1
Version:go1.14.2
OS:darwin

goroutine(ゴルーチン)の終了条件は

関数の処理が終わる。
returnで抜ける。
runtime.Goexit() を実行する

おまけ appleのこのOSって、darwinっていうんだ!

*wikiより
Darwin(ダーウィン)はアップルが開発するUnix系のPOSIX準拠オペレーティングシステム (OS) である。 技術的にはNEXTSTEPからOPENSTEPに続く流れを汲み、Mach 3.0+BSDをベースとし、一部の機能は他のBSD系OSからも取り入れている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0