Files
mruby-mruby/mrbgems/mruby-task/examples/task_pass.rb
Yukihiro "Matz" Matsumoto 0ea429e29e mruby-task: fix sleep from root context to use real wall-clock time
when sleep is called from root context (not within a task), it was
instantly advancing the simulated tick counter instead of actually
delaying. this caused task_pass.rb example to run tasks 0-5 instantly
without proper delays between iterations.

fixed by using clock_gettime() to track elapsed real time and sleeping
in 1ms intervals. also clear switching_ flag when returning from root
context sleep to prevent unwanted context switches.

removed find_earliest_wakeup_tick() function and time-advancing logic
from task_run_one_iteration() as real delays are now handled by sleep
itself.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 12:23:36 +09:00

22 lines
313 B
Ruby

$global = 0
task = Task.new do
10.times do |i|
puts "Task #{i} is running"
$global += 1
sleep 1
end
end
while true
sleep 0.5
if $global < 6
puts "Global variable is less than 6, waiting..."
Task.pass
else
break
end
end
puts "Global variable reached 6, exiting loop"
Task.run