Files
Yukihiro "Matz" Matsumoto 2a124a704f mruby-task: add comprehensive examples demonstrating task features
added six new examples:
- simple.rb: basic task creation and execution
- priority.rb: priority-based scheduling
- suspend_resume.rb: manual task control
- inspection.rb: task status and inspection methods
- statistics.rb: scheduler monitoring with Task.stat
- producer_consumer.rb: task coordination pattern

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 17:44:31 +09:00

48 lines
946 B
Ruby

# Suspend and Resume Example
# Demonstrates manual task control
puts "=== Suspend/Resume Demo ==="
puts
$counter = 0
# Create a worker task
worker = Task.new(name: "worker") do
10.times do |i|
puts "Worker: iteration #{i}, counter=#{$counter}"
$counter += 1
sleep 0.2
end
puts "Worker: finished!"
end
# Create a controller task
controller = Task.new(name: "controller") do
sleep 0.5
puts "\nController: Suspending worker..."
worker.suspend
puts "Controller: Worker status = #{worker.status}"
sleep 1
puts "\nController: Incrementing counter while worker is suspended..."
5.times do
$counter += 10
puts "Controller: counter=#{$counter}"
sleep 0.2
end
puts "\nController: Resuming worker..."
worker.resume
puts "Controller: Worker status = #{worker.status}"
# Wait for worker to finish
worker.join
end
# Run the scheduler
Task.run
puts "\n=== Final counter value: #{$counter} ==="