Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't kill thread when thread is sleeping #43

Open
skrbhn opened this issue Jun 28, 2016 · 1 comment
Open

Can't kill thread when thread is sleeping #43

skrbhn opened this issue Jun 28, 2016 · 1 comment

Comments

@skrbhn
Copy link

skrbhn commented Jun 28, 2016

class TimerDemo
  def start()
    @timer = Thread.new() {
      Thread.sleep 10
    }
  end

  def stop()
    @timer.kill()
  end
end

th = TimerDemo.new()
th.start()
Thread.sleep 7
th.stop()
while true
  Thread.sleep 7
end

After 10 seconds, it's closed by a unkown error which is from os.

@Dan2552
Copy link

Dan2552 commented Nov 4, 2020

I'm not 100% sure if related to this issue, or another issue entirely -- I'm not seeing any unknown error

but as far as I can tell, either I'm misunderstanding the purpose of kill, or it simply doesn't work

def timeout(time, &blk)
  t2 = Thread.new(&blk)

  t1 = Thread.new do
    Thread.sleep(time)
    t2.kill
    puts "t2 should be killed; alive = #{t2.alive?}"
  end

  t2.join
  puts "t2 finished"
  t1.kill
end

timeout(2) do
  puts "waiting for char"
  char = STDIN.getc
  puts "this shouldn't run"
end
puts "after timeout"

prints:

waiting for char
t2 should be killed; alive = true

and if the enter key is pressed after waiting:

waiting for char
t2 should be killed; alive = true

this shouldn't run
t2 finished
after timeout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants