Multithreading not working

Hello,

I want to have another backgorund thread running in my ruby plugin but threads doesn’t seem to work.
I tried Thread.new{…} but the code inside the brackets won’t get executed.

any ideas?

Thanks!

It probably depends on what you are trying to do within the thread. Can you post some sample code?

for example saving my model.

What could anyone want to be doing, whilst the model is in the midst of being saved ? (We never want anything to interfere with this!)

Okay, then for example just print something (puts “test”)

You need to understand that embedded Ruby is different than a system Ruby instance running in it’s own process.

The SketchUp engine controls when Ruby runs. When SketchUp needs to do something exclusively, it will.

Another thing is that for the most part, SketchUp plugins are event-driven code. They have class and module instances that define callback methods, that just wait for the SketchUp engine to call them passing some information.

But yes, also you can write some linear commands, such as exporters and importers, or geometry create commands,… but they just do their thing, and then finish.


So, yes you can write standard IO (ie, via puts) but SketchUp may control when.

There is an alternate thread-like C++ mechanism exposed via the API.

See:

The SketchUp API doesn’t support being called from anything other than the main thread.

Additionally, there is an issue with Ruby threads when embedded into an application like SketchUp, they don’t run unless the main thread is busy - which then defeat the purpose of them. If you really need threading, and aren’t interacting with the API, then I’d recommend you use a Ruby C extension and do your expensive calculations in native threads.

3 Likes