Stack level too deep error

I tried to call an external library which worked in older version of
Ruby
On Rails with DL, but I couldn’t get it working in newer version of Ruby
On
Rails using fiddle, the error message is “stack level too deep”. I tried
several new versions of Ruby without success. After some digging, it
seems
that it works in Ruby script, but not if I use thread, see codes posted
below, I suspect that it might be an issue with stack size (when use
thread) ? I actually tried to comment out codes to generate newer
external
library to find out where the crash is, and find that the program
crashes
at a loop with 20000 loops when computing entries for a 4X20000 matrix.
Any
advice to fix this? Thanks!

Working ruby script:

require ‘fiddle/import’

module Libm
extend Fiddle::Importer
dlload ‘/Library/WebServer/smart/lib/libsamplesize.dylib’
extern ‘long Calculate(long, double, double, char*)’
end

errmsg = " "

puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)

Not working in thread:

require ‘fiddle/import’

module Libm
extend Fiddle::Importer
dlload ‘/Library/WebServer/smart/lib/libsamplesize.dylib’
extern ‘long Calculate(long, double, double, char*)’
end

puts “Started At #{Time.now}”
errmsg = " "
thr1=Thread.new{ puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)}
thr1.join
puts “End at #{Time.now}”