Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:50:28+00:00 2026-05-16T23:50:28+00:00

I’m writing a C extension, providing an interface between Ruby and an asynchronous I/O

  • 0

I’m writing a C extension, providing an interface between Ruby and an asynchronous I/O library. When running the tests over my code, I frequently get errors including (but not limited to):

[BUG] cross-thread violation in rb_thread_schedule()

Asynchronous IO means my C extension will need to deliver messages to ruby from multiple threads (not the main interpreter thread). How do I avoid these thread-safety violations, in the process?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:50:28+00:00Added an answer on May 16, 2026 at 11:50 pm

    For ruby 1.8.x the only way to avoid the error is the obvious one — only invoke the Ruby/C API from the main interpreter thread. I believe this applies to ruby 1.9.x as well, but I haven’t worked with it and don’t know how its native thread support might change things. Instead of having multiple native threads directly invoke the API, you need to use the producer/consumer pattern to deliver requests from your secondary native threads to your code in the main interpreter thread. And ideally do this while not unnecessarily blocking other Ruby green threads. If you look at the ruby implementation, the ruby green thread scheduler is essentialy a select() loop. This suggests the following overall structure:

    • Create a pipe or other IPC mechanism which provides a real select()-able file descriptor.
    • Spawn native threads and provide them with the write end of the pipe.
    • In the main interpreter thread, enter an event loop which calls rb_thread_wait_fd() on the read end of the pipe. This will allow the ruby green thread scheduler to run other green threads.
    • When your secondary native threads have requests for the main thread, they queue them and also write to the pipe, waking up the green thread running your event loop.

    See rb_io_sysread() (implementation of IO#sysread) for what is probably the simplest clean IO-using function in the ruby code base.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.