05 Sep 2011, 17:31
Generic-user-small

Balaji Salunke (1 post)

We are testing thread coperation for our embedded ruby. We have a C ruby extension implementented the following methods 1. longRunningMethod() 2. shortRunningMethod().

Here is a code for the checking the thread cooperation

//file test.rb

require ‘mymodule’

$a = 0; obj = MyModule::MyClass.new t1 = Thread.new{$a = obj.veryLongRunningOperation(); puts “doneLong”} sleep 1 $a = obj.shortOperation() puts “doneShort” t1.join

We have ensured that the longRunningMethod takes more than 1sec(5sec) for execution using nested for loops As per our understanding, the shortRunningMethod should be completed first and then longRunningMethod.

However we observed this only when we did not have any sleep command. But when we introduced the “sleep 1” statement. The longRunningMethod gets executed first and then shortRunningMethod

Anyone would give us the pointers as to why the sleep statement produces this result?

[We are using ruby 1.8.6]

  You must be logged in to comment