Giter Site home page Giter Site logo

Comments (4)

chrisseaton avatar chrisseaton commented on June 10, 2024

Yes looks like it's run while holding the lock.

synchronize do
# If the executor is shut down, reject this task
return handle_fallback(*args, &task) unless running?
ns_execute(*args, &task)
true
end

from concurrent-ruby.

chrisseaton avatar chrisseaton commented on June 10, 2024
Task 4 running on 'main'
Task 1 running on 'worker-1'
Task 4 complete
Task 5 running on 'main'
Task 1 complete
Task 2 running on 'worker-1'
Task 5 complete
Task 7 running on 'main'
Task 2 complete
Task 3 running on 'worker-1'
Task 7 complete
Task 9 running on 'main'
Task 3 complete
Task 6 running on 'worker-1'
Task 9 complete
Task 6 complete
Task 8 running on 'worker-1'
Task 8 complete
Task 10 running on 'worker-1'
Task 10 complete

That's what we want, isn't it?

from concurrent-ruby.

chrisseaton avatar chrisseaton commented on June 10, 2024

Here's a deterministic test.

require 'concurrent-ruby'

log = Queue.new

executor = Concurrent::FixedThreadPool.new(1, max_queue: 2, fallback_policy: :caller_runs)

worker_unblocker = Concurrent::CountDownLatch.new(1)
executor_unblocker = Concurrent::CountDownLatch.new(1)

# Block the worker thread
executor << proc { worker_unblocker.wait }

# Fill the queue
executor << proc { log.push :queued }
executor << proc { log.push :queued }

t = Thread.new {
  # Block in a caller_runs job
  executor << proc { executor_unblocker.wait; log.push :unblocked }
}

# Wait until the caller_runs job is blocked
Thread.pass until t.status == 'sleep'

# Now unblock the worker thread
worker_unblocker.count_down
executor_unblocker.count_down

executor.shutdown
executor.wait_for_termination

# Ideally, we will see the queued jobs run before the caller_runs job unblocks
p [log.pop, log.pop, log.pop]

# [:unblocked, :queued, :queued] bad
# [:queued, :queued, :unblocked] good

Phew that was hard.

I have a PR that achieves that, if I get feedback that is is the desired behaviour.

from concurrent-ruby.

jonmast avatar jonmast commented on June 10, 2024

Sorry I'm so slow to follow up but this is perfect, thanks @chrisseaton

from concurrent-ruby.

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.