202301

20230130日志

2023年1月30日
2023
202301, 思考

思考 # 让所有类都同时具有一个新属性并不破坏原有代码 新建一个类, 包含新属性, 万能属性Object存放旧类

20230131日志

2023年1月30日
2023
202301, 算法, 月度总结

算法 # 矩阵对角线: 就是行index m*n的二位矩阵与一维的坐标转换, 在reshape时使用 (i, j) = i*n + j 阅读总结 # 新年新气象 思想斗争的武器: 行为心理学 英语语法复习 golang 100 mistakes 复习 算法每天2-5个 单词竞赛

20230129日志

2023年1月29日
2023
202301, 命题

命题 # 条件放在左侧,结果放在右侧 p是q的充分条件, p->q p是q的必要条件, p<-q p是q的重复必要条件, p<->q p的重复必要条件是q, q<->p 小范围能推出大范围

20230128日志

2023年1月28日
2023
202301, 排列组合, latex

数学 # 排列组合 $$P(n, r) = \frac {n!}{(n-r)!}$$ $$C(n, r) = \frac {n!}{(n-r)!r!}$$ P(n, r)的含义:n 个元素中 r 个元素全排列,不考虑剩余的 n-r 个元素的顺序. 既 n!/(n-r)! C(n, r)的含义: 首先在P(n, r)的基础上,也不考虑 r 个元素的顺序, 既 n!/(n-r)!r!

20230127日志

2023年1月27日
2023
202301, 助动词, 情态动词, 系动词, 从句, coding, session, ThreadLocal, 引用

english # 助动词 # 辅助主动词 不能像助动词一样单独作谓语动词 除此以外,情态动词能够表意,也归在助动词内 有些单词除了作为助动词,还可以作为实义动词 基本助动词 # 没有实际含义 be The rabbit is eating a carrot do The reabbit does like coins 构成强调 have The rabbit has eaten a carrot 情态助动词 # 有实际含义 can/could I can kill a wolf. 表能力 Could I borrow your book? 表请求, could 虽然是过去式, 但没有时间上的含义 Anyting can happen. ...

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

...