Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
Ruby threads class exercise

I'm doing some exercises to understand the use of threads on Ruby. It seems to be some concepts that I'm not getting in a class problem I'm trying to do.

In a market, a farmer harvests a product and puts it in an exhibitor. An employee takes the product and brings it to the client's counter. Make two Ruby threads put and take products from the exhibitor.

Here's my solution, but it doesen't work:

require 'thread'

mutex = Mutex.new
cv = ConditionVariable.new

orchard = ["Letuce", "Tomato", "Potato"]
exhibitor = []
counter = []

farmer = Thread.new do
    mutex.synchronize do
        puts "Harvesting..."

        product = huerta.pop()

        puts "Placing the product in an exhibitor..."

        exhibitor.push(product)

        cv.signal
    end
end

employee = Thread.new do
    mutex.synchronize do
        cv.wait(mutex)

        puts "Taking product from exhibitor..."

        product = exhibitor.pop()

        puts "Placing product in the counter..."

        counter.push(product)
    end
end

employee.join
farmer.join

The idea behind the code is that, both threads running at the same time, when the farmer thread ends sends a signal that's intercepted by the employee threads, that's waiting for it to do it's thing.

And I get the following output:

Harvesting...
Placing the product in an exhibitor...
Traceback (most recent call last):
        1: from pruebahilos.rb:38:in `<main>'
pruebahilos.rb:38:in `join': No live threads left. Deadlock? (fatal)
2 threads, 2 sleeps current:0x0000557b0cf8ca50 main thread:0x0000557b0ce98050
* #<Thread:0x0000557b0cec6d78 sleep_forever>
   rb_thread_t:0x0000557b0ce98050 native:0x00007fadd7b13ec0 int:0
   pruebahilos.rb:38:in `join'
   pruebahilos.rb:38:in `<main>'
* #<Thread:0x0000557b0d0b5850 pruebahilos.rb:24 sleep_forever>
   rb_thread_t:0x0000557b0cf8ca50 native:0x00007fadd4324700 int:0
    depended by: tb_thread_id:0x0000557b0ce98050
   pruebahilos.rb:26:in `sleep'
   pruebahilos.rb:26:in `wait'
   pruebahilos.rb:26:in `block (2 levels) in <main>'
   pruebahilos.rb:25:in `synchronize'
   pruebahilos.rb:25:in `block in <main>'
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The error happens because when you create the employee thread and call join on it the farmer's thread has likely finished its job already and quit (that's what "No live threads left" error tries to say us).

Try changing the order in which you create your threads (create an employee thread before the farmer's one) - it should work then.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda