
本文旨在帮助开发者理解和解决Go并发程序中常见的死锁问题,特别是当程序抛出 "throw: all goroutines are asleep - deadlock!" 错误时。我们将分析导致死锁的常见原因,并提供修改后的代码示例,展示如何通过缓冲通道和runtime.Gosched()来避免死锁,同时强调并发编程中确定性和可预测行为的重要性。
在Go语言中,死锁通常发生在多个goroutine(Go的并发执行单元)相互等待对方释放资源的情况下。由于所有goroutine都在等待,没有一个能够继续执行,导致程序永久阻塞。 常见的死锁场景包括:
提供的代码创建了三个goroutine,它们通过多个通道相互通信。Routine1是发起者,它向Routine2和Routine3发送消息,并期望接收响应。Routine2和Routine3则根据接收到的消息进行处理,并可能向其他goroutine发送消息。
原始代码中存在多个潜在的死锁点,主要是由于以下原因:
以下是一些可以避免死锁的方法:
缓冲通道允许发送者在通道未满时发送数据,而无需立即等待接收者。这可以减少goroutine之间的依赖性,从而降低死锁的风险。
command12 := make(chan int, 10) // 创建一个缓冲大小为10的通道
default分支允许select语句在没有case可以立即执行时执行一些操作,而不是永久阻塞。可以使用runtime.Gosched()来让出CPU时间片,允许其他goroutine运行。
import "runtime"
// ...
select {
case cmd1 := <-response12:
{
// ...
}
case cmd2 := <-response13:
{
// ...
}
default:
runtime.Gosched() // 让出CPU时间片
}以下是修改后的代码片段,展示了如何使用缓冲通道和runtime.Gosched()来避免死锁:
package main
import (
"fmt"
"math/rand"
"runtime"
"time"
)
func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
rand.Seed(time.Now().UnixNano())
z12 := 200
z13 := 200
m12 := false
m13 := false
y := 0
for i := 0; i < 20; i++ {
y = rand.Intn(100)
if y == 0 {
fmt.Println(z12, " z12 STATE SAVED")
fmt.Println(z13, " z13 STATE SAVED")
y = 0
command12 <- y
command13 <- y
for !m12 || !m13 {
select {
case cmd1 := <-response12:
z12 = cmd1
if z12 != 0 {
fmt.Println(z12, " z12 Channel Saving.... ")
y = rand.Intn(100)
command12 <- y
}
if z12 == 0 {
m12 = true
fmt.Println(" z12 Channel Saving Stopped ")
}
case cmd2 := <-response13:
z13 = cmd2
if z13 != 0 {
fmt.Println(z13, " z13 Channel Saving.... ")
y = rand.Intn(100)
command13 <- y
}
if z13 == 0 {
m13 = true
fmt.Println(" z13 Channel Saving Stopped ")
}
default:
runtime.Gosched()
}
}
m12 = false
m13 = false
}
if y != 0 {
if y%2 == 0 {
command12 <- y
}
if y%2 != 0 {
command13 <- y
}
select {
case cmd1 := <-response12:
{
z12 = cmd1
fmt.Println(z12, " z12")
}
case cmd2 := <-response13:
{
z13 = cmd2
fmt.Println(z13, " z13")
}
default:
runtime.Gosched()
}
}
}
close(command12)
close(command13)
}
func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
rand.Seed(time.Now().UnixNano())
z21 := 200
z23 := 200
m21 := false
m23 := false
for i := 0; i < 20; i++ {
select {
case x, open := <-command12:
{
if !open {
return
}
if x != 0 && !m23 {
z21 = x
fmt.Println(z21, " z21")
}
if x != 0 && m23 {
z21 = x
fmt.Println(z21, " z21 Channel Saving ")
}
if x == 0 {
m21 = true
if m21 && m23 {
fmt.Println(" z21 and z23 Channel Saving Stopped ")
m23 = false
m21 = false
}
if m21 && !m23 {
z21 = x
fmt.Println(z21, " z21 Channel Saved ")
}
}
}
case x, open := <-response23:
{
if !open {
return
}
if x != 0 && !m21 {
z23 = x
fmt.Println(z23, " z21")
}
if x != 0 && m21 {
z23 = x
fmt.Println(z23, " z23 Channel Saving ")
}
if x == 0 {
m23 = true
if m21 && m23 {
fmt.Println(" z23 Channel Saving Stopped ")
m23 = false
m21 = false
}
if m23 && !m21 {
z23 = x
fmt.Println(z23, " z23 Channel Saved ")
}
}
}
default:
runtime.Gosched()
}
if !m23 && !m21 {
y := rand.Intn(100)
if y%2 == 0 {
if y == 0 {
y = 10
response12 <- y
}
}
if y%2 != 0 {
if y == 0 {
y = 10
response23 <- y
}
}
}
if m23 && !m21 {
y := rand.Intn(100)
response12 <- y
}
if !m23 && m21 {
y := rand.Intn(100)
command23 <- y
}
}
close(response12)
close(command23)
}
func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
rand.Seed(time.Now().UnixNano())
z31 := 200
z32 := 200
m31 := false
m32 := false
for i := 0; i < 20; i++ {
select {
case x, open := <-command13:
{
if !open {
return
}
if x != 0 && !m32 {
z31 = x
fmt.Println(z31, " z21")
}
if x != 0 && m32 {
z31 = x
fmt.Println(z31, " z31 Channel Saving ")
}
if x == 0 {
m31 = true
if m31 && m32 {
fmt.Println(" z21 Channel Saving Stopped ")
m31 = false
m32 = false
}
if m31 && !m32 {
z31 = x
fmt.Println(z31, " z31 Channel Saved ")
}
}
}
case x, open := <-command23:
{
if !open {
return
}
if x != 0 && !m31 {
z32 = x
fmt.Println(z32, " z32")
}
if x != 0 && m31 {
z32 = x
fmt.Println(z32, " z32 Channel Saving ")
}
if x == 0 {
m32 = true
if m31 && m32 {
fmt.Println(" z32 Channel Saving Stopped ")
m31 = false
m32 = false
}
if m32 && !m31 {
z32 = x
fmt.Println(z32, " z32 Channel Saved ")
}
}
}
default:
runtime.Gosched()
}
if !m31 && !m32 {
y := rand.Intn(100)
if y%2 == 0 {
response13 <- y
}
if y%2 != 0 {
response23 <- y
}
}
if m31 && !m32 {
y := rand.Intn(100)
response13 <- y
}
if !m31 && m32 {
y := rand.Intn(100)
response23 <- y
}
}
close(response13)
close(response23)
}
func main() {
command12 := make(chan int, 10)
response12 := make(chan int, 10)
command13 := make(chan int, 10)
response13 := make(chan int, 10)
command23 := make(chan int, 10)
response23 := make(chan int, 10)
go Routine1(command12, response12, command13, response13)
go Routine2(command12, response12, command23, response23)
Routine3(command13, response13, command23, response23)
time.Sleep(5 * time.Second) // 保持程序运行一段时间,以便观察goroutine的执行
}注意事项:
尽可能简化goroutine的状态管理。避免使用复杂的逻辑和多个状态变量,这可以降低代码的复杂性,并减少死锁的风险。
并发程序应该具有确定性和可预测的行为。这意味着程序的输出应该只取决于输入,而不取决于goroutine的执行顺序。避免使用竞态条件和不确定性操作,这可以提高代码的可维护性和可靠性。
解决Go并发程序中的死锁问题需要深入理解goroutine和通道的工作原理,并采取适当的措施来避免死锁的发生。使用缓冲通道、添加default分支到select语句、简化状态管理以及确保确定性和可预测的行为是避免死锁的有效方法。在编写并发程序时,应该始终考虑死锁的风险,并采取相应的预防措施。
以上就是解决Go并发程序中的死锁问题:深入分析与实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号