Skip to content

Commit

Permalink
Refactor GoPool for thread safety and efficiency
Browse files Browse the repository at this point in the history
- Added mutex and condition variable to GoPool struct for thread safety.
- Replaced time.Sleep with condition variable Wait in dispatch method to avoid busy waiting.
- Added locking and unlocking around operations that modify workerStack to ensure thread safety.
- Signaled condition variable after pushing a worker back to the stack.
- Ensured all workers are done before releasing resources in Release method.
- Commented out pool.Release() in BenchmarkGoPool test.
- Changed the number of workers in BenchmarkGoPool test from 10000 to 1e4 for readability.

Signed-off-by: Daniel Hu <[email protected]>
  • Loading branch information
daniel-hutao committed Jul 21, 2023
1 parent bbc50dc commit de737e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
23 changes: 21 additions & 2 deletions gopool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gopool

import "time"
import (
"sync"
)

type Task func()

Expand All @@ -9,6 +11,8 @@ type GoPool struct {
MaxWorkers int
workerStack []int
taskQueue chan Task
mutex sync.Mutex
cond *sync.Cond
}

func NewGoPool(maxWorkers int) *GoPool {
Expand All @@ -18,6 +22,7 @@ func NewGoPool(maxWorkers int) *GoPool {
workerStack: make([]int, maxWorkers),
taskQueue: make(chan Task, 1e6),
}
pool.cond = sync.NewCond(&pool.mutex)
for i := 0; i < maxWorkers; i++ {
worker := newWorker()
pool.Workers[i] = worker
Expand All @@ -34,26 +39,40 @@ func (p *GoPool) AddTask(task Task) {

func (p *GoPool) Release() {
close(p.taskQueue)
p.cond.L.Lock()
for len(p.workerStack) != p.MaxWorkers {
p.cond.Wait()
}
p.cond.L.Unlock()
for _, worker := range p.Workers {
close(worker.TaskQueue)
}
p.Workers = nil
p.workerStack = nil
}

func (p *GoPool) popWorker() int {
p.mutex.Lock()
workerIndex := p.workerStack[len(p.workerStack)-1]
p.workerStack = p.workerStack[:len(p.workerStack)-1]
p.mutex.Unlock()
return workerIndex
}

func (p *GoPool) pushWorker(workerIndex int) {
p.mutex.Lock()
p.workerStack = append(p.workerStack, workerIndex)
p.mutex.Unlock()
p.cond.Signal()
}

func (p *GoPool) dispatch() {
for task := range p.taskQueue {
p.cond.L.Lock()
for len(p.workerStack) == 0 {
time.Sleep(time.Millisecond)
p.cond.Wait()
}
p.cond.L.Unlock()
workerIndex := p.popWorker()
p.Workers[workerIndex].TaskQueue <- task
}
Expand Down
4 changes: 2 additions & 2 deletions gopool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGoPool(t *testing.T) {
func BenchmarkGoPool(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)
pool := NewGoPool(10000)
pool := NewGoPool(1e4)

b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -32,7 +32,7 @@ func BenchmarkGoPool(b *testing.B) {
}
}
wg.Wait()
pool.Release()
// pool.Release()
}

func BenchmarkGoroutines(b *testing.B) {
Expand Down

0 comments on commit de737e9

Please sign in to comment.