Git Product home page Git Product logo

agent's Issues

unpredictable results in the final count in fiber_safe_counter.cr when change loop

My mistakes, Pleaase ignore this issue! I forget change 10 to 100 in 10.times { done.receive }.

hello, the raw fiber_safe_counter.cr work fine! But when I change loop number from 10 to 100 as below, I got unpredictable results in the final count :

$ cat fiber_safe_counter.cr  #
# compile with `crystal build -Dpreview_mt examples/fiber_safe_counter.cr`
# run with `CRYSTAL_WORKERS=8 ./fiber_safe_counter`
require "../src/agent"

done = Channel(Nil).new(10)
counter = Agent.new(0)

t0 = Time.utc

(1..100).each {
  spawn {
    (1..1024).each {
      counter.update { |x| x + 1 }
    }
    done.send nil
  }
}
10.times { done.receive }

t1 = Time.utc
puts t1-t0

puts counter.get # => 1024 * 10

$ crystal-1.3.2-1/bin/crystal build -Dpreview_mt fiber_safe_counter.cr 
$CRYSTAL_WORKERS=2 ./fiber_safe_counter
00:00:00.136953628
96824

$CRYSTAL_WORKERS=2 ./fiber_safe_counter
00:00:00.150021704
100215

$CRYSTAL_WORKERS=1 ./fiber_safe_counter
00:00:00.180829373
102400

my system is Ubuntu 18.04.4 LTS

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.