# Are Ruby Threads working on Windows yet?

**URL:** <https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690>\
**Category:** Ruby API\
**Created:** [November 20, 2018, 8:08pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690 "2018-11-20T20:08:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [November 20, 2018, 8:08pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/1 "2018-11-20T20:08:58Z")

</div>

I was testing and causing an insurmountable memory spikes, I had to Force Quit, so I wrapped it in an Threads array…

```ruby
      threads = []
      images.each do |img|
        in_file = File.join(dir, img)
        threads << Thread.new { upscale_double(in_file, args[0],args[1]) }
      end
      threads.each { |thr| thr.join }

```

the memory still maxed out, but the CPU used all cores and the previously **‘this is never going to complete’** call, resolved itself in a reasonable timeframe…

Although similar has happened on a mac since I started hacking, I was surprised by the multi core activity and then wondered if windows PC’s even dealt with ruby threads **yet** …

john

---

<div class="post-metadata">

**Author:** ![thomthom](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/thomthom/32/12606_2.png) [@thomthom](https://forums.sketchup.com/u/thomthom)\
**Post date:** [November 21, 2018, 1:02pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/2 "2018-11-21T13:02:14Z")

</div>

No, Ruby threads in SketchUp isn’t behaving properly.

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [November 25, 2018, 4:41pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/3 "2018-11-25T16:41:05Z")

</div>

> [@john\_drivenupthewall](#):
>
> Although similar has happened on a mac since I started hacking, I was surprised by the multi core activity and then wondered if windows PC’s even dealt with ruby threads **yet** …

Currently in ruby trunk (what will be Ruby 2.6), both Windows & MacOS are being fully tested and some of the tests are run parallel, which uses pipes & threads to run testing concurrently.

Windows builds have been doing so since 2.5 was in development.

Not necessarily helpful when compatibility with 2.2 and/or 2.0 is needed…

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [December 7, 2018, 3:32am UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/4 "2018-12-07T03:32:37Z")

</div>

Friday afternoon. Tired of other things. Attached are two files.

`thread_pool_q.rb` is a generic module that allows one to run a task in threads. It requires a defined collection of objects to perform an operation on, the operation is performed via a passed block. The number of threads defaults to the number of cores available, and can be increased via a multiplier.

If `ary` is the collection, and `meth` is the method defining the operation, one can compare threads versus serial via:

```auto
ThreadPoolQ.run(ary) { |obj| meth(obj) }
  -- and --
ary.each { |obj| meth(obj) }

```

`thread_pool_test.rb` is a file that demos `ThreadPoolQ`.

Note that the ‘binding’ for the block is created outside of the `ThreadPoolQ`, so any variables available in the method/operation can be used. As to practical uses in SU, I haven’t experimented with threads (in SU) for a long time…

Tested on stand-alone Windows Rubies, 2.0 & trunk.

[thread\_pool\_q.rb](https://forums.sketchup.com/uploads/short-url/ePMmuwo2pZrwJrV9RhpHN1tcBfP.rb) (1.1 KB)  
[thread\_pool\_test.rb](https://forums.sketchup.com/uploads/short-url/3DJooRIrmPdPHWjemYz1lCWKaqH.rb) (605 Bytes)

---

<div class="post-metadata">

**Author:** ![thomthom](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/thomthom/32/12606_2.png) [@thomthom](https://forums.sketchup.com/u/thomthom)\
**Post date:** [December 10, 2018, 7:49pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/5 "2018-12-10T19:49:26Z")

</div>

Is this using Ruby’s own threads in any way?

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [December 11, 2018, 1:22am UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/6 "2018-12-11T01:22:49Z")

</div>

> [@thomthom](#):
>
> Is this using Ruby’s own threads in any way?

Yes. Both John’s example and `thread_pool_q.rb` use `Thread.new`.

`thread_pool_q.rb` shows one way to limit the number of threads, yet perform a repetitive task using threads.

SU probably won’t like multiple threads each creating DrawingElements at the same time, so using threads in SU is limited…

---

<div class="post-metadata">

**Author:** ![thomthom](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/thomthom/32/12606_2.png) [@thomthom](https://forums.sketchup.com/u/thomthom)\
**Post date:** [December 12, 2018, 3:05pm UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/7 "2018-12-12T15:05:29Z")

</div>

No, the SketchUp API is not thread-safe. Should never attempt to call the SketchUp API from another thread.  
And we still have the issue of Ruby threads not working properly inside of SketchUp. Still don’t know what causes it, but Ruby threads won’t run without the main thread being busy/actively working on something.

---

<div class="post-metadata">

**Author:** ![MSP\_Greg](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/msp_greg/32/206066_2.png) [@MSP\_Greg](https://forums.sketchup.com/u/MSP_Greg)\
**Post date:** [March 1, 2019, 2:14am UTC](https://forums.sketchup.com/t/are-ruby-threads-working-on-windows-yet/81690/8 "2019-03-01T02:14:00Z")

</div>

John,

Okay, kind of a dead thread, but…

I loaded the trial version of 2019, and the above code works. Timing:

0.743 using threads, 6.100 unthreaded

So, not calling anything in the SU API, threads do work on windows.

I did notice that the code had a glob pattern (`ary = Dir["C:/Greg/GitHub/ruby/*.c"]`) in `thread_pool_test.rb` that needs to be changed to run on one’s system. Just needs to be a pattern with a bunch of files…
