Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:26:42+00:00 2026-05-22T23:26:42+00:00

I have a computation that can be divided into independent units and the way

  • 0

I have a computation that can be divided into independent units and the way I’m dealing with it now is by creating a fixed number of threads and then handing off chunks of work to be done in each thread. So in pseudo code here’s what it looks like

# main thread
work_units.take(10).each {|work_unit| spawn_thread_for work_unit}

def spawn_thread_for(work)
  Thread.new do
    do_some work
    more_work = work_units.pop
    spawn_thread_for more_work unless more_work.nil?
  end
end

Basically once the initial number of threads is created each one does some work and then keeps taking stuff to be done from the work stack until nothing is left. Everything works fine when I run things in irb but when I execute the script using the interpreter things don’t work out so well. I’m not sure how to make the main thread wait until all the work is finished. Is there a nice way of doing this or am I stuck with executing sleep 10 until work_units.empty? in the main thread

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:26:43+00:00Added an answer on May 22, 2026 at 11:26 pm

    If you modify spawn_thread_for to save a reference to your created Thread, then you can call Thread#join on the thread to wait for completion:

    x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
    a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
    x.join # Let the threads finish before
    a.join # main thread exits...
    

    produces:

    abxyzc
    

    (Stolen from the ri Thread.new documentation. See the ri Thread.join documentation for some more details.)

    So, if you amend spawn_thread_for to save the Thread references, you can join on them all:

    (Untested, but ought to give the flavor)

    # main thread
    work_units = Queue.new # and fill the queue...
    
    threads = []
    10.downto(1) do
      threads << Thread.new do
        loop do
          w = work_units.pop
          Thread::exit() if w.nil?
          do_some_work(w)
        end
      end
    end
    
    # main thread continues while work threads devour work
    
    threads.each(&:join)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard that computation results can be very sensitive to choice of random number
I have an Interface called IStep that can do some computation (See Execution in
I have some python code that currently performs expensive computation by performing the computation
Are there any asm instructions that can speed up computation of min/max of vector
I have a some computation program. Now, this program is a single-thread, I need
I have a method in a django model that does a computation relative to
I have heard of Python's GIL problem, which states that there can only be
In some crunching number program, I have a function which can be just 1
I have a computation that calculates a resulting percentage based on certain input. But
I have an entity that has a generic integer parameter fs_in_khz that can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.