我有一个带有goroutine的程序,我们将调用mainRoutine来锁定资源,另一方面,我们将其他被触发的goroutines命名为goroutine-0 goroutine-1 goroutine-2 ....此例程尝试获取锁,在mainRoutine停止后,我需要我的其他goroutine以同步方式获取锁,我的意思是我想要goroutine-0然后goroutine-1 ... 我为解决此问题所做的工作是花费时间片。时间以goroutine启动时使用的time.Now()填充,并使用sync.Cond。 一些代码示例来说明:
package main
import (
"fmt"
"sync"
"time"
)
func condition(myTime time.Time, timeSlice []time.Time) bool {
for _, v := range timeSlice {
if myTime.After(v) {
return false
}
}
return true
}
func removeFromSlice(myTime time.Time, timeSlice []time.Time) {
var place int
for i, v := range timeSlice {
if myTime.Equal(v) {
place = i
break
}
}
timeSlice = append(timeSlice[:place], timeSlice[place+1:]...)
}
func main() {
var m sync.Mutex
c := sync.NewCond(&m)
c.L.Lock()
fmt.Println("Locker locked")
go func() {
time.Sleep(time.Second * 1)
c.L.Unlock()
fmt.Println("Locker unlocked")
}()
var wg sync.WaitGroup
var timeSlice []time.Time
wg.Add(100)
for i := 0; i < 100; i++ {
now := time.Now()
timeSlice = append(timeSlice, now)
time.Sleep(time.Nanosecond * 1) // ensure there's at leat 1 nanosec of diff between 2 time.Now
go func(i int, myTime time.Time) {
fmt.Printf("Before %d %d\n", i, myTime.Unix())
c.L.Lock()
for !condition(myTime, timeSlice) {
c.Wait()
}
c.L.Unlock()
removeFromSlice(myTime, timeSlice)
c.Broadcast()
wg.Done()
fmt.Printf("After done %d\n", i)
}(i, now)
}
wg.Wait()
fmt.Println("Hello, playground")
}
我认为这不是做这种看起来确实很棘手的事情的正确方法,还有更好的方法吗?
-编辑- 在回答@Vorsprung之后,我认为我的案例中最好的方法是制作一个函数切片,该函数总是称为该切片的第一个元素
package main
import (
"fmt"
"sync"
)
func makeFunc(id int) func() {
return func() {
fmt.Printf("called %d\n", id)
}
}
func main() {
var wg sync.WaitGroup
var funcSlice []func()
var m sync.Mutex
for i := 0; i < 5; i++ {
funcSlice = append(funcSlice, makeFunc(i))
wg.Add(1)
go func() {
defer wg.Done()
m.Lock()
defer m.Unlock()
funcSlice[0]()
funcSlice = funcSlice[1:]
}()
}
wg.Wait()
fmt.Println("finished")
}
答案 0 :(得分:2)
给goroutines一个内部ID,然后让它们依次调用。下面的示例说明如何工作
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
var c [5]chan int
for i := range c {
c[i] = make(chan int)
wg.Add(1)
go func(id int) {
defer wg.Done()
f := <-c[id]
fmt.Println("called from ", f, ". My id ", id)
if id < 4 {
fmt.Println(id+1, " next")
c[id+1] <- id
}
fmt.Println("ending ", id)
}(i)
}
c[0] <- 99
wg.Wait()
fmt.Println("bye")
}