golang代码 channel模拟锁
mutex := make(chan struct{}, 1) // 容量必须为1 increase := func() { mutex <- struct{}{} // 加锁 ...... <-mutex // 解锁 } -------------------------------------------------------------------------------- mutex := make(chan struct{}, 1) mutex <- struct{}{} // 此行是必需的 counter := 0 increase := func() { <-mutex // 使用接收操作来加锁 counter++ mutex <- struct{}{} // 解锁 }
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号