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

  • SEARCH
  • Home
  • 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 7196357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:45:46+00:00 2026-05-28T20:45:46+00:00

Ruby 1.9 is supposed to have native threads, and GIL is supposed to lift

  • 0

Ruby 1.9 is supposed to have native threads, and GIL is supposed to lift if some threads enters native code (like GUI toolkit main loop or C implementation of some Ruby lib).

But if i start following simple code sample that displays GUI in main thread and do some basic math in separate thread – the GUI will hang out badly, try to resize window to see it yourself. I have checked with different GUI toolkit, Qt (qtbindings gem) – it behaves exactly same. Tested with Ruby 1.9.3-p0 on Windows 7 and OSX 10.7

require 'tk'
require 'thread'
Thread.new { loop { a = 1 } }
TkRoot.new.mainloop()

Same code in Python works fine without any GUI hangs:

from Tkinter import *
from threading import *
class WorkThread( Thread ) :
  def run( self ) :
    while True :
      a = 1
WorkThread().start()
Tk().mainloop()

What i’m doing wrong?

UPDATE

It seems where is no such problem on Ubuntu linux, so my question is mainly about Windows and OSX.

UPDATE

Some people points out that where is no such problem on OSX. So i assembled out a step-by-step guide to isolate and reproduce a problem:

  1. Install OSX 10.7 Lion via “Recovery” function. I used our test department MB139RS/A mac mini for test.
  2. Install all updates. The system will look like this:
    enter image description here
  3. Install latest ActiveTcl from activestate.com, in my case it’s ActiveTcl 8.5.11 for OSX.
  4. Download and unpack latest Ruby source code. In my case it’s Ruby 1.9.3-p125. Compile it and install replacing system Ruby (commands below). You will end up with latest ruby with built-in Tk support:
    enter image description here
  5. Create a test.rb file with code from my example and run it. Try resizing a window – you will see terrible lags. Remove thread from code, start and try resizing a window – lags are gone. I recorded a video of this test.

Ruby compilation commands:

./configure --with-arch=x86_64,i386 --enable-pthread --enable-shared --with-gcc=clang --prefix=/usr
make
sudo make install
  • 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-28T20:45:47+00:00Added an answer on May 28, 2026 at 8:45 pm

    This hang can be caused by C code of Ruby bindings in Toolkit. As you know, ruby threads have a global lock : the GIL. It seems that mixing between Ruby bindings’ C thread, Tk C thread and Pure Ruby thread is not going well.

    There’s a documented workaround for a similar case, you can try to add those lines before require 'tk' :

    module TkCore 
      RUN_EVENTLOOP_ON_MAIN_THREAD = true
    end
    

    Graphical toolkit needs a main thread in order to refresh graphical elements. If your thread is in an intensive computation, your thread is requesting heavily the lock and so it is interfering with toolkit’s thread.

    You can avoid use of sleep trick if you want. In Ruby 1.9, you can use Fiber, Revactor or EventMachine. According to oldmoe, Fibers seems to be quite fast.

    You can also keep Ruby threads if you can use IO.pipe. That’s how parallel tests were implemented in ruby 1.9.3. It seems to be a good way to workaround Ruby threads and GIL limitations.

    Documentation shows a sample usage :

    rd, wr = IO.pipe
    
    if fork 
      wr.close
      puts "Parent got: <#{rd.read}>"
      rd.close
      Process.wait
    else 
      rd.close
      puts "Sending message to parent"
      wr.write "Hi Dad"
      wr.close
    end
    

    The fork call initiates two processes. Inside if, you are in the parent process. Inside else, you are in the child. The call to Process.wait closes child process.
    You can, for instance, try to read from your child in your main gui loop, and only close & wait for the child when you have received all the data.

    EDIT: You’ll need win32-process if you choose to use fork() under Windows.

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

Sidebar

Related Questions

I have a struct in my Ruby code that looks somewhat like this Parameter
I have a Ruby 1.8.6 script that is supposed to take a filename that
In Ruby, everything is supposed to be an object. But I have a big
I have a simple ruby server something like : app = Proc.new do |env|
If nuget packages are supposed to be like ruby gems I would assume that
I currently have ruby version 1.8.2 in my machine and I would like to
I am a ruby newbie and have been trying Sinatra for quite some time
I have this code: $(input#autocomplete).autocomplete({ source: [c++, java, php, coldfusion, javascript, asp, ruby] });
I have some code that seems to execute improperly when it is serialized and
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to

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.