Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“实现阻塞读且并发安全的map”答案补充 #61

Open
JiahaoWei-RH opened this issue Mar 7, 2022 · 1 comment
Open

“实现阻塞读且并发安全的map”答案补充 #61

JiahaoWei-RH opened this issue Mar 7, 2022 · 1 comment

Comments

@JiahaoWei-RH
Copy link

JiahaoWei-RH commented Mar 7, 2022

我注意到https://github.com/lifei6671/interview-go/blob/master/question/q010.md中缺少了Rdfunc的实现,我完成了,并且经过了简单的测试。

func (m *Map) Rd(key string, timeout time.Duration) interface{} {
	e, ok := m.c[key]
	if !ok {
		// create channel
		m.c[key] = &entry{
			isExist: false,
			ch:      make(chan struct{}),
		}
	}
	// 阻塞
	e, _ = m.c[key]
	if e.ch != nil {
		select {
		case <-e.ch:
		case <-time.After(timeout):
		}
	}
	return m.c[key].value
}
@CraigMChen
Copy link

没加锁?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants