Skip to content

Instantly share code, notes, and snippets.

@devlights
Created September 25, 2023 09:13
Show Gist options
  • Save devlights/394060dc52fb53a7f69bab2b57278b65 to your computer and use it in GitHub Desktop.
Save devlights/394060dc52fb53a7f69bab2b57278b65 to your computer and use it in GitHub Desktop.
tasksetコマンドを使って特定のCPUコアでプログラムを動かす

通常

$ go build
$ ./app
GOMAXPROC=12
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t2: 0
t3: 0
t3: 1
t3: 2
t3: 3
t3: 4
t2: 1
t2: 2
t2: 3
t2: 4

taskset指定

$ taskset -c 0 ./app
GOMAXPROC=1
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t2: 0
t2: 1
t2: 2
t2: 3
t2: 4
t3: 0
t3: 1
t3: 2
t3: 3
t3: 4
package main
import (
"fmt"
"os"
"runtime"
"sync"
)
const (
LOOP_COUNT = 5
)
func init() {
fmt.Printf("GOMAXPROC=%d\n", runtime.GOMAXPROCS(0))
}
func main() {
var (
ch = make(chan string)
wg = sync.WaitGroup{}
fn = func(wg *sync.WaitGroup, ch chan<- string, prefix int) {
defer wg.Done()
for i := 0; i < LOOP_COUNT; i++ {
ch <- fmt.Sprintf("t%d: %d", prefix, i)
}
}
)
wg.Add(3)
go fn(&wg, ch, 1)
go fn(&wg, ch, 2)
go fn(&wg, ch, 3)
go func() {
defer close(ch)
wg.Wait()
}()
for v := range ch {
fmt.Fprintln(os.Stderr, v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment