From e4b4bfd946250ce4a340d355f220628b369501f9 Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Mon, 12 Jun 2023 15:29:19 +0800 Subject: [PATCH] fix: polling cpu load --- workpool/workpool.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/workpool/workpool.go b/workpool/workpool.go index aedf1af..1c6c100 100644 --- a/workpool/workpool.go +++ b/workpool/workpool.go @@ -104,13 +104,23 @@ func (p *WorkPool) startQueue() { } func (p *WorkPool) waitTask() { + var c int + for { - runtime.Gosched() // 出让时间片 + c++ + if p.IsDone() { if atomic.LoadInt32(&p.isQueTask) == 0 { break } } + + if c <= 100 { + runtime.Gosched() // 出让时间片 + continue + } + + time.Sleep(20 * time.Microsecond) // 20us, cpu cost < 1% } }