Ruby/DL - cross-thread violation on rb_thread_schedule()

I think I’m a little over my head here and was hoping someone more
experienced can explain this to me, a novice trying to learn ruby.

I’m currently trying to use a win32 DLL to get a live feed of data from
a server. With the help of some people form this forum, I was able to
make a module using ruby/dl to use the functions on the DLL. I’m
currently able to log on to the server, request snapshot data and log
off. Now I’m trying to use the live feed functions from the library.
These are functions where I pass a few parameters and a callback
function. The function then returns a pointer to the session object and
calls the callback function whenever an update occurs on their end.

I tried to make a simple implementation where I log on, request for a
live feed, and keep the program running and print out the data I get
from the server using the callback function I provide.

So here’s what I did to test things out:

feed_library.rb is my module using ruby/dl to access the dll functions,
as well as the structures I use to convert the pointers to records

require ‘feed_library’

my callback for the live feed

FEED_CALLBACK = DL.callback(‘0PL’) { |result_list, count| }
if result_list
record = FeedRecord.new(result_list)
puts record
end
}

handle used to request for a feed

handle = FeedLibrary::log_on(“user”, “password”)

session = get_live_feed(handle, FEED_CALLBACK)

my attempt to keep this running and waiting on the callback:

while true

end

Whenever I run this I get the cross-thread violation on
rb_thread_schedule() error. Been trying to read on other topics on this
but I couldn’t fully grasp the discussion. Can anyone explain what’s
happening and what would be the best approach in getting the feed?

Thanks for taking the time to read this :slight_smile:

Well been trying to read up on this topic and I found this from a
thread(http://programming.itags.org/programming/346457/):

“To my knowledge, a cross-thread violation occurs when a piece of code
(which exists on a different stack altogether, i.e. running within a
POSIX or native thread alongside the Ruby interpreter in the same
process) makes use of Ruby’s C language API.
I have encountered this when I was embedding a Ruby interpreter inside a
C program. Ruby was running inside a POSIX thread and therefore had its
own stack. When the C program made use of Ruby’s C language API, the
entire process would crash with that cross-thread violation error.”

So does that mean when my DLL library initiates the callback that I pass
it runs it in another stack? Would this be causing the error? If so is
there a way to have the callback initiated in the same thread as the
ruby interpreter?

Well I noticed I got the error when I try to use my structure to convert
my ptrData result_list:

my callback for the live feed

FEED_CALLBACK = DL.callback(‘0PL’) { |result_list, count| }
if result_list
record = FeedRecord.new(result_list)
puts record
end
}

Now I’m trying to figure out how to not use the structure or find
another way I can read the structure passed to the call back.

Anyone here can explain why I’m exactly getting this type of error when
I use my structure and what I can do? I was happy that I was making
progress using ruby/dl but now been stuck at this for days and can’t
find anything that can help me. :frowning:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs