sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Ruby 2.0.0 Threads in SU | how to keep a subthread running?

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 6 Posters 1.6k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • tt_suT Offline
      tt_su
      last edited by

      Yes, we have observed this. We're not sure why as Ruby claims native threads in 2.0 as oppose to the "green" threads in 1.8 - but yet something is amiss. Might be related to the Ruby interpreter running as embedded in SketchUp - which Ruby isn't really designed to. If you run standalone you will probably find it works fine.

      1 Reply Last reply Reply Quote 0
      • tt_suT Offline
        tt_su
        last edited by

        Btw, when sharing variables between threads you probably want to use Mutex. Not that it solves this issue though.

        1 Reply Last reply Reply Quote 0
        • S Offline
          SR20VET
          last edited by

          Ok tt_su, I might have to go the hard way then, and use native threads in a c++ extension.

          1 Reply Last reply Reply Quote 0
          • tt_suT Offline
            tt_su
            last edited by

            As things stand right now that would be the route to go.

            We have a GitHub repo with the Ruby libs we use in SketchUp along with Visual Studio and Xcode projects set up: https://github.com/SketchUp/ruby-c-extension-examples

            1 Reply Last reply Reply Quote 0
            • S Offline
              slbaumgartner
              last edited by

              According to Programming Ruby 1.9 and 2.0 section 12.2, Ruby 1.9 and 2.0 encountered trouble with numerous existing libraries that were not thread-safe. "So, Ruby compromises: it uses native operating system threads but operates only a single thread at a time." That is very likely what you are seeing.

              1 Reply Last reply Reply Quote 0
              • tt_suT Offline
                tt_su
                last edited by

                So much for "multi-threading"... sigh
                Still, the worker threads appear to act differently under standalone Ruby vs SketchUp embedded Ruby. So we are suffering from some side effect of how Ruby deals with its threads.

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  What about Ruby 2.1 ?

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    Anton_S
                    last edited by

                    Here are Ruby 2.1 release notes : https://github.com/ruby/ruby/blob/v2_1_0/NEWS
                    I haven't found anything with threads though.

                    1 Reply Last reply Reply Quote 0
                    • S Offline
                      SR20VET
                      last edited by

                      To make sure the subthread is running, we have to put the main thread in a sleep state.
                      This is because Ruby does switch to another thread (if existing) when a thread goes into a sleep state.

                      Now, is there a way to automatically put the main thread in a sleep state when the user is not interacting with SU? Once the user starts to interact (mouse movement, click, ...) the sleep state should be interrupted.

                      I'm not aware of such an event in SU.

                      For now this:

                      message_pump = UI.start_timer(0.1, true) do
                      	sleep(0.1)
                      end
                      

                      does help a bit, but ofcourse, this drastically affects SU performance. Even sleeping for 0.01 seconds does..

                      1 Reply Last reply Reply Quote 0
                      • tt_suT Offline
                        tt_su
                        last edited by

                        I wouldn't go the route of Ruby threads for now. If you really need it then use native threads in C extension.

                        One scenario where it might be used is where you do multiple calculations in parallel and don't mind locking the main thread. You can then start your worker threads and join them with the main one. But other than that I would not rely on Ruby threads due to the current state of them.

                        1 Reply Last reply Reply Quote 0
                        • S Offline
                          SR20VET
                          last edited by

                          Yep, I've taken the native threads path. Got the thread running, got even an http server running in that thread. But now, GIL is killing me. Can't call ruby from that second native thread...

                          1 Reply Last reply Reply Quote 0
                          • tt_suT Offline
                            tt_su
                            last edited by

                            Yea, that's another limitation. You can do calculations and such in other threads. But Ruby calls must be done some the main thread.

                            1 Reply Last reply Reply Quote 0
                            • icehuliI Offline
                              icehuli
                              last edited by

                              @sr20vet said:

                              Yep, I've taken the native threads path. Got the thread running, got even an http server running in that thread. But now, GIL is killing me. Can't call ruby from that second native thread...

                              May I ask what exactly you are trying to do? I think you can call ruby from that second native thread.
                              One important point is that the ruby function needs to be involved in the ruby thread, i.e. the SU thread. So in the c-extension there should be two threads, one is the same SU thread, the other is your second native thread. All ruby codes need to run within the SU thread.

                              1 Reply Last reply Reply Quote 0
                              • 1 / 1
                              • First post
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement