You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
342 B
32 lines
342 B
package golock
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestLock(t *testing.T) {
|
|
|
|
var counter int
|
|
|
|
var l = NewLock()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 10; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if !l.Lock() {
|
|
t.Error("上锁失败")
|
|
return
|
|
}
|
|
counter++
|
|
t.Log(counter)
|
|
l.Unlock()
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|