Blog

20230122日志

2023年1月22日
2023
202301, condition

golang # sync.cond # type Donation struct { mu sync.RWMutex balance int } donation := &Donation{} // Listener goroutines f := func(goal int) { donation.mu.RLock() for donation.balance < goal { donation.mu.RUnlock() donation.mu.RLock() } fmt.Printf("$%d goal reached\n", donation.balance) donation.mu.RUnlock() } go f(10) go f(15) // Updater goroutine go func() { for { time.Sleep(time.Second) donation.mu.Lock() donation.balance++ donation.mu.Unlock() } }() cpu空转 type Donation struct { balance int ch chan int } donation := &Donation{ch: make(chan int)} // Listener goroutines f := func(goal int) { for balance := range donation. ...

20230121日志

2023年1月21日
2023
202301, goroutine停止, for循环下goroutine闭包, select多个channel, noticication channel, nil channel, buffered or not buffered channel, waitGroup, append是否会导致Data race

golang # 传播不合适的context # func handler(w http.ResponseWriter, r *http.Request) { response, err := doSomeTask(r.Context(), r) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } go func() { err := publish(r.Context(), response) // Do something with err }() writeResponse(response) } type detach struct { ctx context.Context } func (d detach) Deadline() (time.Time, bool) { return time.Time{}, false } func (d detach) Done() <-chan struct{} { return nil } func (d detach) Err() error { return nil } func (d detach) Value(key any) any { return d. ...

20230120日志

2023年1月20日
2023
202301, 线程, goroutine, data race, race condition, 内存模型, context

golang #

基本概念 #

不能直接创建Thread, 但是可以创建goroutine; os线程由os调度, goroutine在os线程内由goruntime调度; GOMAXPROCS定义产生多少OS线程, 在有block时, 还会生成新的OS线程, GOMAXPROCS默认等于机器的逻辑核心数; goroutine的状态, Executing: 在OS线程上运行, Runnable: 等待进入Executing, Waitting: stopped并等待一些事情完成(system call, sync, wait mutex); 1.14之前只会在chan send/recieve, i/o, wait mutext时发生context switch, 之后的版本会将运行超过10ms的goroutine标记为preemptible, 并有机会context-switched off被其他goroutine替换

goroutine_arrange

...

20230118日志

2023年1月18日
2023
202301, 单词

English # velocity n. 速度 insect n. 昆虫 swarm n. 一大群 v. 成群飞行 a swarm of insects bounce n. 反弹 v. 弹射 radius n. 半径 v. 成弧形 view radius 视觉半径 cohesion n. 内聚 alignment n. 对其 separation n. 分离 quadratic n. 二次方程式 adj. 二次的 minus adj. 小于0的 n. 负号 plus or minus 增减; 正负 acceleration n. 加速度 jittery adj. 紧张的, 敏感的 market jittery advertise v 为…做广告 to advertise cheap food flip v. ...

20230115日志

2023年1月15日
2023
202301, hugo

hugo # Hugo 基于golang template, 快速建站 github pages 无法接入百度,谷歌搜索 vercel 托管静态站点 域名 A记录:指向ip地址,CNAME: 指向其他域名, 参考阿里云域名服务页面

20230114日志

2023年1月14日
2023
202301, 非谓语动词

英语 #

非谓语动词(没有时间变化) #

  1. 做主语,宾语,表语,宾语补足语,定语,状语
  2. 加to为完全不定式,动词原形是裸不定式
  3. The rabbit liked to eat carrot
  4. To be or not to be, that is the problem
...