当前位置:网站首页>几道关于golang并发的面试题
几道关于golang并发的面试题
2022-08-01 23:32:00 【youngqqcn】
请问运行以下程序会怎样? ( )
- A: 不能编译
- B: 输出 main --> A --> B --> C
- C: 输出 main
- D: panic
package main
import (
"fmt"
"sync"
)
var mu sync.Mutex
var chain string
func main() {
chain = "main"
A()
fmt.Println(chain)
}
func A() {
mu.Lock()
defer mu.Unlock()
chain = chain + " --> A"
B()
}
func B() {
chain = chain + " --> B"
C()
}
func C() {
mu.Lock()
defer mu.Unlock()
chain = chain + " --> C"
}
,
,
,
,
,
,
,
答案:panic
死锁
为什么?
// 引自: https://www.jianshu.com/p/679041bdaa39
Mutex(互斥锁)
- Mutex 为互斥锁,Lock() 加锁,Unlock() 解锁
- 在一个 goroutine 获得 Mutex 后,其他 goroutine 只能等到这个 goroutine 释放该 Mutex
- 使用 Lock() 加锁后,不能再继续对其加锁,直到利用 Unlock() 解锁后才能再加锁
- 在 Lock() 之前使用 Unlock() 会导致 panic 异常
- 已经锁定的 Mutex 并不与特定的 goroutine 相关联,这样可以利用一个 goroutine 对其加锁,再利用其他 goroutine 对其解锁
- 在同一个 goroutine 中的 Mutex 解锁之前再次进行加锁,会导致死锁
- 适用于读写不确定,并且只有一个读或者写的场景
请问运行以下程序会怎样? ( )
- A: 不能编译
- B: 输出 1
- C: 程序hang住
- D: panic
package main
import (
"fmt"
"sync"
"time"
)
var mu sync.RWMutex
var count int
func main() {
go A()
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
count++
fmt.Println(count)
}
func A() {
mu.RLock()
defer mu.RUnlock()
B()
}
func B() {
time.Sleep(5 * time.Second)
C()
}
func C() {
mu.RLock()
defer mu.RUnlock()
}
,
,
,
,
,
,
,
,
,
,
,
,
答案:panic
死锁
为什么?
// 引自: https://www.jianshu.com/p/679041bdaa39
RWMutex(读写锁)
- RWMutex 是单写多读锁,该锁可以加多个读锁或者一个写锁
- 读锁占用的情况下会阻止写,不会阻止读,多个 goroutine 可以同时获取读锁
- 写锁会阻止其他 goroutine(无论读和写)进来,整个锁由该 goroutine 独占
- 适用于读多写少的场景
Lock() 和 Unlock()
- Lock() 加写锁,Unlock() 解写锁
- 如果在加写锁之前已经有其他的读锁和写锁,则 Lock() 会阻塞直到该锁可用,为确保该锁可用,已经阻塞的 Lock() 调用会从获得的锁中排除新的读取器,即写锁权限高于读锁,有写锁时优先进行写锁定
- 在 Lock() 之前使用 Unlock() 会导致 panic 异常
RLock() 和 RUnlock()
- RLock() 加读锁,RUnlock() 解读锁
- RLock() 加读锁时,如果存在写锁,则无法加读锁;当只有读锁或者没有锁时,可以加读锁,读锁可以加载多个
- RUnlock() 解读锁,RUnlock() 撤销单词 RLock() 调用,对于其他同时存在的读锁则没有效果
- 在没有读锁的情况下调用 RUnlock() 会导致 panic 错误
- RUnlock() 的个数不得多余 RLock(),否则会导致 panic 错误
以下程序运行会发生吗? ( )
- A: 不能编译
- B: 无输出,正常退出
- C: 程序hang住
- D: panic
package main
import (
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(time.Millisecond)
wg.Done()
wg.Add(1)
}()
wg.Wait()
}
,
,
,
,
,
,
,
,
,
答案:panic
, 会出现panic: sync: WaitGroup is reused before previous Wait has returned
panic: sync: WaitGroup is reused before previous Wait has returned
goroutine 1 [running]:
sync.(*WaitGroup).Wait(0x45f280?)
/usr/local/go/src/sync/waitgroup.go:138 +0x85
main.main()
/home/yqq/mine/master-go/interview/6-interview-golang/solution31.go:14 +0x85
边栏推荐
- Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
- D - Linear Probing- 并查集
- 在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
- 测试岗月薪5-9k,如何实现涨薪到25k?
- nodejs--process
- excel remove all carriage return from a cell
- How do programmers solve online problems gracefully?
- Spark Sql之union
- Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
- prim生成树
猜你喜欢
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
6134. Find the closest node to the given two nodes - force double hundred code
ICLR 2022最佳论文:基于对比消歧的偏标签学习
分享10套开源免费的高品质源码,免费源码下载平台
cmd command
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
数据增强--学习笔记(图像类,cnn)
Spark Sql之join on and和where
Secondary Vocational Network Security Competition B7 Competition Deployment Process
【参营经历贴】2022网安夏令营
随机推荐
Chapter 11 Working with Dates and Times
What is CICD excuse me
Nacos配置中心之加载配置
numpy.around
【C语言进阶】文件操作(二)
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
[Recommended books] The first self-driving technology book
Flink学习第四天——完成第一个Flink 流批一体案例
Chapter 11 Working with Dates and Times
欧拉路径与欧拉回路
Additional Features for Scripting
如何更好的理解的和做好工作?
Calculate the distance between two points
vscode hide menu bar
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
D - Linear Probing- 并查集
When using DocumentFragments add a large number of elements
请问什么是 CICD
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution