icexin's recent timeline updates
icexin

icexin

V2EX member #97481, joined on 2015-02-14 19:43:08 +08:00
icexin's recent replies
copliot 买过年包,个人感觉 cursor 和 copilot 的一个区别是:cursor 可以在代码行中间进行补全,copilot 只能从结尾补全。另外 cursor 的 tab 模式比较智能,准确率很高,小重构的时候框框 tab 就完事了。
一般是通过 jit 编译到机器码的,具体实现上可以有不经过优化的一遍翻译,也可以有多遍的优化编译。可以参考一下 v8 的 https://v8.dev/blog/liftoff
Nov 30, 2021
Replied to a topic by sunny1688 问与答 关于 golang 碰到的一个问题!
大家回答的点都集中在内存回收上,实际的问题是没有加锁导致的不变式被打破的问题。

实际的 slice 包含 data ,len 和 cap 字段,这些大家也都知道了。slice 结构的不变式是:在任意时刻,data 指向的数据长度都是至少是 len 长度,否则访问 len-1 的数据就会 内存错误。

在题主的代码里面,多个 goroutine 同时对 demo.llist 进行赋值,但因为没有加锁,所以赋值不是原子的,从而会出现一个 goroutine 刚赋值了 data ,还没来得及赋值 data 和 cap 就被其他 goroutine 拿去用了, 破坏了不变式, 从而在扩容的时候就访问了非法内存,从而 panic 。

一段简单代码就可以复现:


package main

import "log"

type T struct {
A, B int
}

func step(t T) T {
if t.B != t.A*2 {
log.Panic(t)
}
x := t.A+1
return T{
A: x,
B: 2*x,
}
}

func main() {
var t = T{
A: 1,
B: 2,
}
for {
go func() {
t = step(t)
}()
}
}
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2553 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 19ms · UTC 12:55 · PVG 20:55 · LAX 05:55 · JFK 08:55
♥ Do have faith in what you're doing.