Code RoomSemaphore lost wakeup
HardPrep Room Coding #2038

Semaphore lost wakeup

Code reviewConcurrencySenior–Staff~30 min

Review this Go bounded semaphore used to cap concurrent uploads. Workers call Acquire before an upload and Release after.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1type Sem struct {
2 mu sync.Mutex
3 cond *sync.Cond
4 avail int
5}
6 
7func NewSem(n int) *Sem {
8 s := &Sem{avail: n}
9 s.cond = sync.NewCond(&s.mu)
10 return s
11}
12 
13func (s *Sem) Acquire() {
14 s.mu.Lock()
15 if s.avail == 0 {
16 s.cond.Wait()
17 }
18 s.avail--
19 s.mu.Unlock()
20}
21 
22func (s *Sem) Release() {
23 s.mu.Lock()
24 s.avail++
25 s.cond.Signal()
26 s.mu.Unlock()
27}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.