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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:14:19+00:00 2026-06-13T16:14:19+00:00

There aren’t many resources on Condition Variables in Ruby, however most of them are

  • 0

There aren’t many resources on Condition Variables in Ruby, however most of them are wrong. Like ruby-doc, tutorial here or post here – all of them suffer with possible deadlock.

We could solve the problem by starting threads in given order and maybe putting some sleep in between to enforce synchronization. But that’s just postponing the real problem.

I rewrote the code into a classical producer-consumer problem:

require 'thread'
queue = []
mutex = Mutex.new
resource = ConditionVariable.new
threads = []

threads << Thread.new do
  5.times do |i|
    mutex.synchronize do 
      resource.wait(mutex)
      value = queue.pop
      print "consumed #{value}\n"
    end
  end
end

threads << Thread.new do
  5.times do |i|
    mutex.synchronize do
      queue << i
      print "#{i} produced\n"
      resource.signal
    end
    sleep(1) #simulate expense
  end
end

threads.each(&:join)

Sometimes you will get this (but not always):

0 produced
1 produced
consumed 0
2 produced
consumed 1
3 produced
consumed 2
4 produced
consumed 3
producer-consumer.rb:30:in `join': deadlock detected (fatal)
        from producer-consumer.rb:30:in `each'
        from producer-consumer.rb:30:in `<main>'

What is the correct solution?

  • 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-13T16:14:20+00:00Added an answer on June 13, 2026 at 4:14 pm

    This is more robust solution with multiple consumers and producers and usage of MonitorMixin, MonitorMixin has a special ConditionVariable with wait_while() and wait_until() methods

    require 'monitor'
    
    queue = []
    queue.extend(MonitorMixin)
    cond = queue.new_cond
    consumers, producers = [], []
    
    for i in 0..5
      consumers << Thread.start(i) do |i|
          print "consumer start #{i}\n"
          while (producers.any?(&:alive?) || !queue.empty?)
            queue.synchronize do
            cond.wait_while { queue.empty? }
            print "consumer #{i}: #{queue.shift}\n"
          end
          sleep(0.2) #simulate expense
        end
      end
    end
    
    for i in 0..3
      producers << Thread.start(i) do |i|
        id = (65+i).chr
        for j in 0..10 do
          queue.synchronize do
            item = "#{j} #{id}"
            queue << item
            print "producer #{id}: produced #{item}\n"
            j += 1
            cond.broadcast
          end
          sleep(0.1) #simulate expense
        end
      end
    end
    
    sleep 0.1 while producers.any?(&:alive?)
    sleep 0.1 while consumers.any?(&:alive?)
    
    print "queue size #{queue.size}\n"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relational database, however, there aren't any foreign keys defined, and many
There are plenty of Libraries to parse XML, but it seems there aren't many
I have a query that selects from two tables, however if there aren't any
I read that there aren't events in ASP.Net MVC. However, I added button and
I see there aren't many Tableau experts floating around StackOverflow, but perhaps someone out
All my searches returned nothing and I find it odd that there aren't any
I've found the class WordnetSynonymParser in org.apache.lucene.analysis.synonym but there aren't examples of its usage
I have searched for examples on idempotent and non-idempotent operations but there aren't much
This is related to another open question of mine . While there aren't any
If I run my web app in VisualStudio, and set cookies, they aren't there

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.