0

0

解决 Go 并发程序中的死锁问题:深入分析与实践

碧海醫心

碧海醫心

发布时间:2025-08-17 21:24:01

|

813人浏览过

|

来源于php中文网

原创

解决 go 并发程序中的死锁问题:深入分析与实践

在 Go 并发编程中,死锁是一个常见且令人头疼的问题。当所有 Goroutine 都处于等待状态,无法继续执行时,Go 运行时会抛出 "throw: all goroutines are asleep - deadlock!" 错误。本文将深入分析一个实际的死锁案例,并提供详细的解决方案,包括使用 runtime.Gosched() 让出 CPU 时间片以及利用缓冲通道来打破僵局。此外,我们还会探讨如何避免在并发程序设计中引入不确定性,以提高程序的可维护性和可靠性。

理解 Go 中的死锁

死锁通常发生在多个 Goroutine 相互等待对方释放资源的情况下。由于每个 Goroutine 都无法继续执行,整个程序就被阻塞了。在 Go 中,通道(channel)是 Goroutine 之间进行通信和同步的主要方式,因此,不正确地使用通道是导致死锁的常见原因。

案例分析

以下代码展示了一个可能导致死锁的并发程序:

package main

import (
    "fmt"
    "math/rand"
    "runtime"
    "time"
)

func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
    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 != true || m13 != true {
                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() // 让出 CPU 时间片
                }

            }

            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() // 让出 CPU 时间片
            }
        }
    }
    close(command12)
    close(command13)
}

func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
    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 != true {
                    z21 = x
                    fmt.Println(z21, "   z21")
                }
                if x != 0 && m23 == true {
                    z21 = x
                    fmt.Println(z21, "   z21 Channel Saving ")
                }
                if x == 0 {
                    m21 = true
                    if m21 == true && m23 == true {
                        fmt.Println(" z21 and z23 Channel Saving Stopped ")
                        m23 = false
                        m21 = false
                    }
                    if m21 == true && m23 != true {
                        z21 = x
                        fmt.Println(z21, "   z21  Channel Saved ")

                    }

                }
            }

        case x, open := <-response23:
            {
                if !open {
                    return
                }
                if x != 0 && m21 != true {
                    z23 = x
                    fmt.Println(z23, "   z21")
                }
                if x != 0 && m21 == true {
                    z23 = x
                    fmt.Println(z23, "   z23 Channel Saving ")
                }
                if x == 0 {
                    m23 = true
                    if m21 == true && m23 == true {
                        fmt.Println(" z23 Channel Saving Stopped ")
                        m23 = false
                        m21 = false
                    }
                    if m23 == true && m21 != true {
                        z23 = x
                        fmt.Println(z23, "   z23  Channel Saved ")
                    }

                }
            }
        default:
            runtime.Gosched() // 让出 CPU 时间片
        }

        if m23 == false && m21 == false {
            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 == true && m21 != true {
            y := rand.Intn(100)
            response12 <- y
        }

        if m23 != true && m21 == true {
            y := rand.Intn(100)
            command23 <- y
        }

    }
    close(response12)
    close(command23)
}

func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
    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 != true {
                    z31 = x
                    fmt.Println(z31, "   z21")
                }
                if x != 0 && m32 == true {
                    z31 = x
                    fmt.Println(z31, "   z31 Channel Saving ")
                }
                if x == 0 {
                    m31 = true
                    if m31 == true && m32 == true {
                        fmt.Println(" z21 Channel Saving Stopped ")
                        m31 = false
                        m32 = false
                    }
                    if m31 == true && m32 != true {
                        z31 = x
                        fmt.Println(z31, "   z31  Channel Saved ")

                    }

                }
            }

        case x, open := <-command23:
            {
                if !open {
                    return
                }
                if x != 0 && m31 != true {
                    z32 = x
                    fmt.Println(z32, "   z32")
                }
                if x != 0 && m31 == true {
                    z32 = x
                    fmt.Println(z32, "   z32 Channel Saving ")
                }
                if x == 0 {
                    m32 = true
                    if m31 == true && m32 == true {
                        fmt.Println(" z32 Channel Saving Stopped ")
                        m31 = false
                        m32 = false
                    }
                    if m32 == true && m31 != true {
                        z32 = x
                        fmt.Println(z32, "   z32  Channel Saved ")

                    }

                }
            }
        default:
            runtime.Gosched() // 让出 CPU 时间片
        }
        if m31 == false && m32 == false {
            y := rand.Intn(100)
            if y%2 == 0 {
                response13 <- y
            }

            if y%2 != 0 {
                response23 <- y
            }
        }

        if m31 == true && m32 != true {
            y := rand.Intn(100)
            response13 <- y
        }

        if m31 != true && m32 == true {
            y := rand.Intn(100)
            response23 <- y
        }

    }
    close(response13)
    close(response23)
}

const bufferSize = 4 // 缓冲大小

func main() {
    rand.Seed(time.Now().UnixNano()) // 初始化随机数生成器

    command12 := make(chan int, bufferSize)
    response12 := make(chan int, bufferSize)
    command13 := make(chan int, bufferSize)
    response13 := make(chan int, bufferSize)
    command23 := make(chan int, bufferSize)
    response23 := make(chan int, bufferSize)

    go Routine1(command12, response12, command13, response13)
    go Routine2(command12, response12, command23, response23)
    Routine3(command13, response13, command23, response23)
}

这段代码创建了三个 Goroutine,它们通过多个通道相互通信。Routine1 是一个发起者,它可以向 Routine2 和 Routine3 发送数据,并期望收到响应。Routine2 和 Routine3 则根据接收到的数据进行处理,并可能向其他 Goroutine 发送数据。

这段代码的复杂性在于它试图模拟一种状态保存机制,当 y 的值为 0 时,Goroutine 会尝试保存当前状态,并与其他 Goroutine 协调。这种复杂的逻辑增加了死锁的可能性。

死锁的原因

在这个例子中,死锁的根本原因是:

  1. 无缓冲通道的阻塞性: 如果一个 Goroutine 尝试向一个无缓冲通道发送数据,但没有其他 Goroutine 准备好接收,那么发送操作将会被阻塞。同样,如果一个 Goroutine 尝试从一个无缓冲通道接收数据,但通道中没有数据,那么接收操作也会被阻塞。
  2. 相互依赖的 Goroutine: Routine1、Routine2 和 Routine3 相互依赖,它们之间的通信需要按照特定的顺序进行。如果任何一个 Goroutine 被阻塞,其他 Goroutine 也可能因为等待而无法继续执行。
  3. 复杂的状态管理: 状态保存机制增加了代码的复杂性,使得 Goroutine 之间的交互更加难以预测。

具体来说,当 Routine1 尝试同时向 command12 和 command13 发送 0 时,如果 Routine2 和 Routine3 没有准备好接收,那么 Routine1 就会被阻塞。此时,如果 Routine2 和 Routine3 又在等待 Routine1 发送其他数据,那么就会形成一个死锁。

解决方案

针对上述死锁问题,可以采取以下几种解决方案:

人民网AIGC-X
人民网AIGC-X

国内科研机构联合推出的AI生成内容检测工具

下载
  1. 使用 runtime.Gosched(): 在 select 语句的 default 分支中调用 runtime.Gosched() 可以让出 CPU 时间片,允许其他 Goroutine 运行。这有助于打破僵局,避免死锁。
  2. 使用缓冲通道: 将无缓冲通道改为缓冲通道可以缓解阻塞问题。缓冲通道允许在没有接收者的情况下发送一定数量的数据,从而减少了 Goroutine 之间的依赖性。
  3. 简化状态管理: 重新设计状态保存机制,使其更加简单和可预测。避免在 Goroutine 之间传递复杂的状态信息,尽量使用原子操作或互斥锁来保护共享数据。
  4. 超时机制: 在 select 语句中使用 time.After 添加超时机制,避免 Goroutine 无限期地等待。

代码改进

以下代码展示了如何使用 runtime.Gosched() 和缓冲通道来改进上述程序:

package main

import (
    "fmt"
    "math/rand"
    "runtime"
    "time"
)

// ... (Routine1, Routine2, Routine3 函数的定义,与之前相同,但添加了 default case 并调用 runtime.Gosched())

const bufferSize = 4 // 缓冲大小

func main() {
    rand.Seed(time.Now().UnixNano()) // 初始化随机数生成器

    command12 := make(chan int, bufferSize)
    response12 := make(chan int, bufferSize)
    command13 := make(chan int, bufferSize)
    response13 := make(chan int, bufferSize)
    command23 := make(chan int, bufferSize)
    response23 := make(chan int, bufferSize)

    go Routine1(command12, response12, command13, response13)
    go Routine2(command12, response12, command23, response23)
    Routine3(command13, response13, command23, response23)

    time.Sleep(5 * time.Second) // 保证所有 Goroutine 运行完成
}

在这个改进后的代码中,我们首先将所有的通道都改为了缓冲通道,并设置了缓冲区大小为 4。这允许 Goroutine 在没有接收者的情况下发送少量数据,从而减少了阻塞的可能性。

其次,我们在 select 语句的 default 分支中添加了 runtime.Gosched() 调用。这使得 Goroutine 在没有其他事件发生时,可以主动让出 CPU 时间片,允许其他 Goroutine 运行。

最后,我们在 main 函数中添加了一个 time.Sleep() 调用,以确保所有的 Goroutine 都有足够的时间运行完成。

注意事项

  1. 缓冲通道的大小: 缓冲通道的大小需要根据实际情况进行调整。如果缓冲区太小,仍然可能导致阻塞;如果缓冲区太大,可能会浪费内存。
  2. runtime.Gosched() 的使用: runtime.Gosched() 应该谨慎使用。过度使用 runtime.Gosched() 可能会降低程序的性能。
  3. 并发程序的确定性: 尽量避免在并发程序中使用随机数或依赖于外部状态的操作。这可以提高程序的可预测性和可维护性。

总结

Go 并发编程中的死锁是一个常见但可以避免的问题。通过理解死锁产生的原因,并采取相应的解决方案,我们可以编写出更加健壮和可靠的并发程序。

在设计并发程序时,应该始终牢记以下几点:

  • 尽量减少 Goroutine 之间的依赖性。
  • 避免在 Goroutine 之间传递复杂的状态信息。
  • 使用原子操作或互斥锁来保护共享数据。
  • 使用缓冲通道来缓解阻塞问题。
  • 谨慎使用 runtime.Gosched()。
  • 尽量提高并发程序的确定性。

通过遵循这些原则,我们可以编写出更加高效、可靠和易于维护的 Go 并发程序。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang channel原理
Golang channel原理

本专题整合了Golang channel通信相关介绍,阅读专题下面的文章了解更多详细内容。

248

2025.11.14

golang channel相关教程
golang channel相关教程

本专题整合了golang处理channel相关教程,阅读专题下面的文章了解更多详细内容。

344

2025.11.17

default gateway怎么配置
default gateway怎么配置

配置default gateway的步骤:1、了解网络环境;2、获取路由器IP地址;3、登录路由器管理界面;4、找到并配置WAN口设置;5、配置默认网关;6、保存设置并退出;7、检查网络连接是否正常。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

223

2023.12.07

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

12

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

563

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

200

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

336

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
10分钟--Midjourney创作自己的漫画
10分钟--Midjourney创作自己的漫画

共1课时 | 0.1万人学习

Midjourney 关键词系列整合
Midjourney 关键词系列整合

共13课时 | 0.9万人学习

AI绘画教程
AI绘画教程

共2课时 | 0.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号