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 7865065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:58:17+00:00 2026-06-02T23:58:17+00:00

I have an algorithm that performs a breadth-first search of resources: def crawl(starting_node) items=[starting_node]

  • 0

I have an algorithm that performs a breadth-first search of resources:

def crawl(starting_node)
  items=[starting_node]
  until items.empty?
    item = items.shift
    kids = item.slow_network_action # takes seconds
    kids.each{ |kid| items << kid }
  end
end

I’d like to use a few concurrent threads to parallelize the slow_network_action.
What’s a reasonable way to do this?

Here’s a technique that works, but I feel certain is not the right approach:

def crawl(starting_node)
  mutex = Mutex.new
  items = [starting_node]
  4.times.map{
    loop do
      unless item=mutex.synchronize{ items.shift }
        sleep LONGER_THAN_LONGEST_NETWORK_ACTION
        break unless item=mutex.synchronize{ items.shift }
      end
      kids = item.slow_network_action
      mutex.synchronize{
        kids.each{ |kid| items << kid }
      }
    end
  }.each(&:join)
end

I’d like to do something like have the threads actually sleep while waiting for an item to be added to the queue, wake up when an item is added, and have all threads exit when everyone is waiting, when none have been added.


This alternate code almost works but for the deadlocks that can (and do) occur, and the total lack of a proper exit strategy:

require 'thread'
def crawl(starting_node)
  items = Queue.new
  items << starting_node
  4.times.map{
    while item=items.shift
      kids = item.slow_network_action
      kids.each{ |kid| items << kid }
    end
  }.each(&:join)
end
  • 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-06-02T23:58:19+00:00Added an answer on June 2, 2026 at 11:58 pm

    This should point you in the right direction:

    require 'monitor'
    
    NUM_THREADS = 4
    
    def crawl(starting_node)
      items = [starting_node]
      items.extend MonitorMixin
      item_cond = items.new_cond
    
      threads = []
      working_threads = 0
      finished = false
    
      NUM_THREADS.times do
        items.synchronize do
          working_threads += 1
        end
        threads << Thread.new do
          item = nil
          kids = []
          loop do
            items.synchronize do
    
              #add any new items to array
              items.concat kids
    
              if (items.empty? && working_threads == 1)
                #all other threads are waiting, and there's no more items
                #to process, so we must be done
                finished = true
              end
    
              #wake up all waiting threads, either to finish or do more work
              #watch out for thundering herds
              item_cond.broadcast unless (items.empty? && !finished)
    
              #wait, but first decrement count of working threads
              #so we can determine when to finish
              working_threads -= 1
              item_cond.wait_while { items.empty? && !finished}
              Thread.exit if finished
              working_threads += 1
    
              #get next item
              item = items.shift
            end
    
            kids = item.slow_network_action
          end
    
        end
      end
    
      threads.each(&:join)
    end
    

    This makes the items array into a monitor and does any synchronization through that, along with an asociated ConditionVariable created from the monitor.

    This is similiar to how a Queue works internally, except that this also checks for when all work is finished (which actually adds a bit of complexity).

    The threads main loop starts with an empty kids array that gets added to items in order to avoid needing two separate synchronized blocks in the loop, and the race conditions that would go with them.

    Note that this uses broadcast which causes all waiting threads to wake, and could potentially cause a thundering herd. I don’t think this should cause any problems here. The alternative would be to add the elements of kids one at a time, and call signal for each one. This would add more complexity for dealing with the case when all work is finished though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two part question Best-Practice I have an algorithm that performs some
I have simple algorithm that clean the whitespace from half string until end. Here
I have converted a relatively simple algorithm that performs a large number of calculations
I have an algorithm that performs the following calculations: ( ( 0.50 * 0
I have an algorithm that generates strings based on a list of input words.
I have an algorithm that recursively makes change in the following manner: public static
I have this algorithm that I want to implement on VB6. Sub Main() dim
If I have an algorithm that takes n log n steps (e.g. heapsort), where
I have to write an algorithm that find the path in DAG with single
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring

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.