Git Product home page Git Product logo

Comments (9)

ardan-bkennedy avatar ardan-bkennedy commented on July 24, 2024

I think this is the data race code. Can you put a link to the specific code you are talking about.

from code.

piperck avatar piperck commented on July 24, 2024

Thanks for reply.
URL:https://github.com/goinaction/code/blob/master/chapter6/listing09/listing09.go
The code output:

Final Counter will print 4

but the book(GO IN ACTION) says :

Final Counter will print 2

Can you fix this?
Thanks

from code.

ardan-bkennedy avatar ardan-bkennedy commented on July 24, 2024

If you are running this on Linux or the Mac, this call runtime.Gosched() will ask the scheduler to swap out the goroutine. This usually happens. I have seen on Windows the scheduler be less preemptive.

If the scheduler does not swap the goroutine, you will continue to see 4. Try this on the playground.

from code.

piperck avatar piperck commented on July 24, 2024

Oh..I running this on Mac and use go version 1.8. It output 4.
You means swap out the goroutine may depends on os?
....I cann't figure it out.

from code.

ardan-bkennedy avatar ardan-bkennedy commented on July 24, 2024

That call tells the scheduler to cooperate and allow the other goroutine to run. Basically forcing the data race. I have never seen that not work on the Mac. If you want we can jump on a ScreenHero

from code.

ardan-bkennedy avatar ardan-bkennedy commented on July 24, 2024

Check this out. https://play.golang.org/p/h9jpgaIO9F

from code.

piperck avatar piperck commented on July 24, 2024

Oh! I figure it out. Normally It output 4. I running again and again .. Finally It output2 once.
I think it always force the data race before.
And I try to use go build -race But nothing happened?

Is this expected??

from code.

ardan-bkennedy avatar ardan-bkennedy commented on July 24, 2024

The race detector should see that immediately. After you build with the race detector, you must run the program. Build does not run.

from code.

piperck avatar piperck commented on July 24, 2024

ok. I see.
Your book is very helpful.
Thanks for your patience.

from code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.