Created
February 10, 2026 16:43
-
-
Save rlcarrca/e29063a3338888dce84aa64d2a37a060 to your computer and use it in GitHub Desktop.
Mutex example snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Counter struct { | |
| mu sync.Mutex | |
| value int | |
| } | |
| func (c *Counter) Increment() { | |
| c.mu.Lock() | |
| c.value++ | |
| c.mu.Unlock() | |
| } | |
| func (c *Counter) Value() int { | |
| c.mu.Lock() | |
| defer c.mu.Unlock() | |
| return c.value | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment