英文:
Golang select wait on both a mutex lock and a channel
问题
在Go语言中,可以使用select
语句同时等待多个通道。例如:
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}
(摘自https://go.dev/tour/concurrency/5)
是否有一种机制可以类似地使用互斥锁(mutex lock),在等待通道或互斥锁可用时进行等待?类似这样:
package main
import "fmt"
import "sync"
func fibonacci(c chan int, mu sync.Mutex) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case mu.Lock():
fmt.Println("locked")
return
}
}
}
func main() {
c := make(chan int)
var mu sync.Mutex
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
mu.Lock()
}()
fibonacci(c, mu)
}
实际的使用场景是,我将一个context传递给一个函数,该函数可能会在互斥锁上等待一段时间,我希望能够通过上下文中止等待。
英文:
In golang it is possible to use a select
statement to wait on multiple channels at a time. For example,
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}
(Lifted from https://go.dev/tour/concurrency/5)
Is there any mechanism to do something similar with a mutex lock, where you wait for either a channel or for a mutex lock to become available? Something like:
package main
import "fmt"
import "sync"
func fibonacci(c chan int, mu sync.Mutex) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case mu.Lock():
fmt.Println("locked")
return
}
}
}
func main() {
c := make(chan int)
var mu sync.Mutex
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
mu.Lock()
}()
fibonacci(c, quit, mu)
}
The actual use scenario is that I'm passing in a context to a function that might be waiting on a mutex lock for a while, and I'd like to be able to abort the wait through the context.
答案1
得分: 2
有没有一种机制可以类似于互斥锁,等待通道或互斥锁可用的情况?
没有。
你必须重新设计,例如通过用基于通道的信号量替换互斥锁。
英文:
> Is there any mechanism to do something similar with a mutex lock, where you wait for either a channel or for a mutex lock to become available?
No.
You must redesign, e.g. by replacing the mutex with a channel based semaphore.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论