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:
- Install OSX 10.7 Lion via "Recovery" function. I used our test department MB139RS/A mac mini for test.
- Install all updates. The system will look like this:
- Install latest ActiveTcl from activestate.com, in my case it's ActiveTcl 8.5.11 for OSX.
- 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:
- 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