1

So, I read here, that we don't have access to threads from javascript. But thats not really true, is it? We do have some sort of thread support from javascript (with workers?). It's not going to be impossible. Is this the issue or am I misunderstanding something?

Community
  • 1
  • 1
Moon4u
  • 127
  • 1
  • 8

1 Answers1

4

Currently there's no plan to implement threads via workers, although a pull-request would probably be accepted for stdlib.

Right now there are shims to allow for simple usage from gems, you can see the code here:

https://github.com/opal/opal/blob/master/stdlib/thread.rb

Elia Schito
  • 989
  • 7
  • 18
  • So, I checked the webworkers. It seems impossible to use them now. The webworkers does not have access to the DOM. They can't even interact with any objects outside their scope. You said that it is possible to use them, from gems. Can we use them in the browser? – Moon4u Dec 02 '14 at 13:00
  • The shims I linked are there just to allow gems that rely on Thread.current to work. Thread.new raises a NotImplementedError. So you can use the shims in the browser, but not real threading. It's not supported by the platform (the browser) in the same way that CRuby doesn't support "fork" on windows. – Elia Schito Dec 03 '14 at 22:52
  • Maybe an alternative could be to use the "promises" implementation we have in the stdlib, see the relative blog article for a tutorial http://opalrb.org/blog/2014/05/07/promises-in-opal/ – Elia Schito Dec 03 '14 at 22:55
  • Didn't even knew about the Promises. Yeah, with Promises and Generators it should be doable. I'm gonna try to use them to implement Fiber class in js. Will write back when I have some results. – Moon4u Dec 05 '14 at 14:55
  • [Fibers](http://www.ruby-doc.org/core-2.1.5/Fiber.html) can easily be implemented with just Generators. The parsing should be a little different than what is happening at the moment. Fibers in ruby are instantiated like this: `Fiber.new do [|params|] ~Fiber Code~ end` after parsing the code should look like this: `Opal.Fiber.$new(function*(params){ ~Fiber Code~ })` Also if function has `Fiber.yield` in it should be created with `function*` instead of just `function`. Is there other place that we can discuss this? Formatting in the comments isn't good enough. – Moon4u Dec 06 '14 at 21:13