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

Sync Chapter: Add explanation on why the concurrent test fails #842

Open
laerson opened this issue Jan 26, 2025 · 0 comments
Open

Sync Chapter: Add explanation on why the concurrent test fails #842

laerson opened this issue Jan 26, 2025 · 0 comments

Comments

@laerson
Copy link

laerson commented Jan 26, 2025

Problem

A common pitfall for people learning about concurrency is why expressions like x = x + 1 can give unexpected results when it happens on multiple threads, since they look like a single instruction.

On the Sync chapter, the implementation of Inc() method isn't concurrently safe, but the reason why can be confusing for someone with no background on assembly:

func (c *Counter) Inc() {
	c.value++
}

Proposed Solution

Highlight that although c.value++ looks like a single instruction, the compiled instruction is actually similar to this:

tmp := x + 1 // instruction A
x = tmp      // instruction B

And then show how the counter can fail depending on the order the goroutines are executed.

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

1 participant