Russell Fulton
Russell Fulton

Reputation: 590

Ruby thread does not execute

I have just added threads to an existing program using a model I have used before but this time the thread did not execute so I added

Thread.new {puts "thread" }
exit

at the top of the program and it exited without printing anything.

Pointers on diagnosing the problem solicited.

Linux (ubuntu) ruby 2.7

Upvotes: 3

Views: 52

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

The given code ..

Thread.new {puts "thread" }
exit

.. is not guaranteed to execute the puts. You must join the thread.

t = Thread.new {puts "thread" }
t.join # block until thread finishes
exit

Upvotes: 3

Related Questions