Nâng CaoGolang iconGolang

Mutex và RWMutex dùng khi nào?

sync.Mutex: Lock()/Unlock() cho exclusive access (1 goroutine tại 1 thời điểm). sync.RWMutex: RLock() cho nhiều readers đồng thời, Lock() cho exclusive write.

  • Dùng khi shared state giữa goroutines.
  • Luôn defer mu.Unlock().
  • Tránh deadlock: lock order nhất quán.

Xem toàn bộ Golang cùng filter theo level & chủ đề con.

Mở danh sách Golang