Skip to content

Commit 0b9266b

Browse files
committed
make writes to shared global hashmap threadsafe
1 parent f39c7cf commit 0b9266b

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

pidusage.go

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime"
1010
"strconv"
1111
"strings"
12+
"sync"
1213
)
1314

1415
// SysInfo will record cpu and memory data
@@ -33,6 +34,7 @@ type fn func(int) (*SysInfo, error)
3334
var fnMap map[string]fn
3435
var platform string
3536
var history map[int]Stat
37+
var historyLock sync.Mutex
3638
var eol string
3739

3840
func wrapper(statType string) func(pid int) (*SysInfo, error) {
@@ -139,7 +141,9 @@ func stat(pid int, statType string) (*SysInfo, error) {
139141
seconds = 1
140142
}
141143

144+
historyLock.Lock()
142145
history[pid] = *stat
146+
historyLock.Unlock()
143147
sysInfo.CPU = (total / seconds) * 100
144148
sysInfo.Memory = stat.rss * pageSize
145149
}

0 commit comments

Comments
 (0)