librelist archives

« back to archive

Let's talk Shoes & threads

Let's talk Shoes & threads

From:
Jesse Edelstein
Date:
2012-02-16 @ 15:10
Hi, all. I have quite a bit of confusion about using threads with
Shoes. I decided that a threaded program was appropriate so the
program can do multiple things at once, and because it might finally
convince Shoes to freaking execute each command I type in order. I
read the threading chapters in The Pickaxe and The Ruby Way. That's
cool, but I'm still clueless, on balance. Take this toy program:

app = Shoes.app do
  def solicit_frank_opinion
    @reasoning = ask("So, why doesn't this thing work?")
  end
end
t1 = Thread.new {app.solicit_frank_opinion; Thread.pass}
t1.join

In *my* mind, this is unambiguous code which should work fine. I want
to create a new Shoes app, then fork a new thread which calls the
method solicit_frank_opinion in that app object. Meanwhile, the main
thread is instructed to hold on until the child thread has collected
an answer. Because threads are scheduled by a VM and t1 has no
critical segments, it seems to me that any background work that the
main thread needs to do will get done; I even explicitly use
Thread.pass to indicate that this would be a great time for the
scheduler to do something else if necessary.

Perfect! Then I run it on OS X and it pops up a blank dialog box and
hangs; on Win 7 it doesn't even get that far, hanging on the main
Shoes app window. So what's this about? Am I just not allowed to use
dialog boxes within a child thread? That seems unlikely at the least.
So what am I missing? How could I rewrite this to still use threads (I
know they're not needed, it's just a minimal example) but, you know,
actually work instead?

Re: [shoes] Let's talk Shoes & threads

From:
Steve Klabnik
Date:
2012-02-16 @ 15:17
This, for example, works:


app = Shoes.app do
  def hello
    para "hello"
  end
end

Thread.new do
  app.hello
end


Changing it to an alert totally bombs for me on OSX as well.

Re: [shoes] Let's talk Shoes & threads

From:
Eric Watson
Date:
2012-02-16 @ 15:40
On Feb 16, 2012, at 9:17 AM, Steve Klabnik wrote:

> This, for example, works:
> 
> 
> app = Shoes.app do
>  def hello
>    para "hello"
>  end
> end
> 
> Thread.new do
>  app.hello
> end
> 
> 
> Changing it to an alert totally bombs for me on OSX as well.

Suggesting the problem is in the native code?

Re: [shoes] Let's talk Shoes & threads

From:
Jesse Edelstein
Date:
2012-02-16 @ 15:53
Are other flavors of Shoes substantially more complete? Should I be
trying out something different, or maybe use a version of Shoes built
on Ruby 1.9, or something? Hoo boy, this project is going poorly.

On Thu, Feb 16, 2012 at 7:40 AM, Eric Watson <wasnotrice@gmail.com> wrote:
> On Feb 16, 2012, at 9:17 AM, Steve Klabnik wrote:
>
>> This, for example, works:
>>
>>
>> app = Shoes.app do
>>  def hello
>>    para "hello"
>>  end
>> end
>>
>> Thread.new do
>>  app.hello
>> end
>>
>>
>> Changing it to an alert totally bombs for me on OSX as well.
>
> Suggesting the problem is in the native code?
>

Re: [shoes] Let's talk Shoes & threads

From:
Steve Klabnik
Date:
2012-02-16 @ 16:03
> Are other flavors of Shoes substantially more complete?

Red is the reference implementation, so technically, none of the rest
of them are 'complete.' Green Shoes has the vast majority of things
implemented.

> Should I be
> trying out something different, or maybe use a version of Shoes built
> on Ruby 1.9, or something?

If you're using Shoes 3, then your Shoes is Ruby 1.9.1.

> Hoo boy, this project is going poorly.

:/

> Suggesting the problem is in the native code?

Yep. If I had to guess, that's the issue. Something's not threadsafe,
apparently.

Jesse, _why left us with a lot of C code of sometimes questionable
quality. It's been a long, slow road of fixing things, and apparently
you've hit an edge case we hadn't so far. I've filed an Issue:
https://github.com/shoes/shoes/issues/184

As for actual _solutions_ to your problem, is the modal aspect of the
'ask' actually necessary? Could you, say, do something like this?

Shoes.app do
  @next_step = stack do
    para "hey there"
  end

  @next_step.hide

  @first_step = stack do
    para "Please type something."
    @input = edit_line

    button "Submit" do
      unless @input.text == ""
        @next_step.show
        @first_step.hide
      end
    end
  end
end

Re: [shoes] Let's talk Shoes & threads

From:
Jesse Edelstein
Date:
2012-02-16 @ 17:06
Thanks for the advice, Steve. Frankly, I just thought I was doing
something really conceptually wrong in Shoes, because I can't wrap my
head around a lot of it. It makes me feel a little more sane to think
it was an error in the toolkit rather than on my part. And luckily I
don't need to bombard the user with a bunch of dialogs; my current
design only has one and it can go in the main thread just fine.

On Thu, Feb 16, 2012 at 8:03 AM, Steve Klabnik <steve@steveklabnik.com> wrote:
>> Are other flavors of Shoes substantially more complete?
>
> Red is the reference implementation, so technically, none of the rest
> of them are 'complete.' Green Shoes has the vast majority of things
> implemented.
>
>> Should I be
>> trying out something different, or maybe use a version of Shoes built
>> on Ruby 1.9, or something?
>
> If you're using Shoes 3, then your Shoes is Ruby 1.9.1.
>
>> Hoo boy, this project is going poorly.
>
> :/
>
>> Suggesting the problem is in the native code?
>
> Yep. If I had to guess, that's the issue. Something's not threadsafe,
> apparently.
>
> Jesse, _why left us with a lot of C code of sometimes questionable
> quality. It's been a long, slow road of fixing things, and apparently
> you've hit an edge case we hadn't so far. I've filed an Issue:
> https://github.com/shoes/shoes/issues/184
>
> As for actual _solutions_ to your problem, is the modal aspect of the
> 'ask' actually necessary? Could you, say, do something like this?
>
> Shoes.app do
>  @next_step = stack do
>    para "hey there"
>  end
>
>  @next_step.hide
>
>  @first_step = stack do
>    para "Please type something."
>    @input = edit_line
>
>    button "Submit" do
>      unless @input.text == ""
>        @next_step.show
>        @first_step.hide
>      end
>    end
>  end
> end

Re: [shoes] Let's talk Shoes & threads

From:
Steve Klabnik
Date:
2012-02-16 @ 17:19
> Thanks for the advice, Steve. Frankly, I just thought I was doing
> something really conceptually wrong in Shoes, because I can't wrap my
> head around a lot of it. It makes me feel a little more sane to think
> it was an error in the toolkit rather than on my part. And luckily I
> don't need to bombard the user with a bunch of dialogs; my current
> design only has one and it can go in the main thread just fine.

Shoes should theoretically be Just Ruby; though of course, GUI stuff
adds some subtle threading things. This is absolutely our error.

Re: [shoes] Let's talk Shoes & threads

From:
ashbb
Date:
2012-02-17 @ 12:27
Hi Jesse, Steve, Eric and folks,

I don't think that this problem is a bug of Shoes. ;-)
It's a spec of Ruby.
# more properly, a spec of thread.

Shoes is a big program, but minimum concept is just this:

  class MiniShoes
    def self.app &blk
      Object.new.instance_eval &blk
    end
  end

So, try out the following 4 one-liners with above MiniShoes.
This is just a ruby program.

  MiniShoes.app{puts 'hello'}             #1
  MiniShoes.app{Thread.new{puts 'hello'}} #2
  MiniShoes.app{puts gets}                #3
  MiniShoes.app{Thread.new{puts gets}}    #4

The above 4 snippets work well.
But in the case of #4, you can't input anything.
Because thread doesn't wait user input for `gets`.

ashbb