# Ruby/DL - cross-thread violation on rb\_thread\_schedule()

**URL:** <https://www.ruby-forum.com/t/ruby-dl-cross-thread-violation-on-rb-thread-schedule/174054>\
**Category:** Ruby\
**Created:** [August 20, 2009, 6:51am UTC](https://www.ruby-forum.com/t/ruby-dl-cross-thread-violation-on-rb-thread-schedule/174054 "2009-08-20T06:51:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 20, 2009, 6:51am UTC](https://www.ruby-forum.com/t/ruby-dl-cross-thread-violation-on-rb-thread-schedule/174054/1 "2009-08-20T06:51:09Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 24, 2009, 11:37am UTC](https://www.ruby-forum.com/t/ruby-dl-cross-thread-violation-on-rb-thread-schedule/174054/2 "2009-08-24T11:37:40Z")

</div>

Well been trying to read up on this topic and I found this from a  
thread([http://programming.itags.org/programming/346457/](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?

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 27, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/ruby-dl-cross-thread-violation-on-rb-thread-schedule/174054/3 "2009-08-27T12:10:13Z")

</div>

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. ☹
