# TkButton#command problem with threads

**URL:** <https://rubytalk.org/t/tkbutton-command-problem-with-threads/11927>\
**Category:** ruby-talk\
**Created:** [3 June 2004 23:17 UTC](https://rubytalk.org/t/tkbutton-command-problem-with-threads/11927 "2004-06-03T23:17:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Steensland](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@Henrik\_Steensland](https://rubytalk.org/u/Henrik_Steensland)\
**Post date:** [3 June 2004 23:17 UTC](https://rubytalk.org/t/tkbutton-command-problem-with-threads/11927/1 "2004-06-03T23:17:27Z")

</div>

I upgraded to FreeBSD's latest version of ruby.

> ruby -v

ruby 1.8.1 (2004-05-02) [i386-freebsd5]

Now, some of my Ruby/Tk applications won't work anymore.  
The problem seems to be this.

cmd = proc{  
&nbsp;&nbsp;puts "a"  
&nbsp;&nbsp;Thread.new{  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 10  
&nbsp;&nbsp;}  
&nbsp;&nbsp;puts "b"  
}

TkButton.new{  
&nbsp;&nbsp;text "Push"  
&nbsp;&nbsp;command cmd  
&nbsp;&nbsp;pack  
}

When running cmd.call i get the printout "a" and "b" quickly. The new Thread  
is just sleeping quietly in the background.  
However, when I push my TkButton, I get the "a", then the whole program  
sleeps for 10 seconds, then I get the "b". So something is really strange  
here. I had no problems with the code above in earlier versions.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [4 June 2004 01:20 UTC](https://rubytalk.org/t/tkbutton-command-problem-with-threads/11927/2 "2004-06-04T01:20:20Z")

</div>

Hi,

> **···**
>
> From: "Henrik Steensland" \<henst374@student.liu.se\>  
> Subject: TkButton#command problem with threads  
> Date: Fri, 4 Jun 2004 08:17:27 +0900  
> Message-ID: \<20040603231722.679E921B19@arcadia.unit.liu.se\>
> 
> > When running cmd.call i get the printout "a" and "b" quickly. The new Thread  
> > is just sleeping quietly in the background.  
> > However, when I push my TkButton, I get the "a", then the whole program  
> > sleeps for 10 seconds, then I get the "b". So something is really strange  
> > here. I had no problems with the code above in earlier versions.
> 
> Thank you for your report. Please test the following patch.  
>   
> --- tcltklib.c.old 2004-06-04 10:15:14.000000000 +0900  
> +++ tcltklib.c 2004-06-04 10:16:17.000000000 +0900  
> @@ -1008,7 +1008,9 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(VALUE)0);  
> #else
> 
> + rb\_thread\_critical = Qfalse;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = rb\_eval\_string\_protect(arg-\>string, &status);  
> + rb\_thread\_critical = Qtrue;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (status) {  
> &nbsp;&nbsp;&nbsp;char \*errtype, \*buf;  
> &nbsp;&nbsp;&nbsp;int errtype\_len, len;  
> @@ -1288,9 +1290,13 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct cmd\_body\_arg \*arg;  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE ret;  
> + int thr\_crit\_bup;
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DUMP1("call ip\_ruby\_cmd\_core");  
> + thr\_crit\_bup = rb\_thread\_critical;  
> + rb\_thread\_critical = Qfalse;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = rb\_apply(arg-\>receiver, arg-\>method, arg-\>args);  
> + rb\_thread\_critical = thr\_crit\_bup;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DUMP1("finish ip\_ruby\_cmd\_core");
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ret;  
>   
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Henrik\_Steensland](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@Henrik\_Steensland](https://rubytalk.org/u/Henrik_Steensland)\
**Post date:** [4 June 2004 09:38 UTC](https://rubytalk.org/t/tkbutton-command-problem-with-threads/11927/3 "2004-06-04T09:38:56Z")

</div>

Thank you very much for your patch.  
Now it works nicely.

> **···**
>
> > -----Original Message-----  
> > From: Hidetoshi NAGAI [mailto:nagai@ai.kyutech.ac.jp]  
> > Sent: Friday, June 04, 2004 3:20 AM  
> > To: ruby-talk ML  
> > Subject: Re: TkButton#command problem with threads
> > 
> > Hi,
> > 
> > From: "Henrik Steensland" \<henst374@student.liu.se\>  
> > Subject: TkButton#command problem with threads  
> > Date: Fri, 4 Jun 2004 08:17:27 +0900  
> > Message-ID: \<20040603231722.679E921B19@arcadia.unit.liu.se\>  
> > \> When running cmd.call i get the printout "a" and "b"  
> > quickly. The new  
> > \> Thread is just sleeping quietly in the background.  
> > \> However, when I push my TkButton, I get the "a", then the whole  
> > \> program sleeps for 10 seconds, then I get the "b". So something is  
> > \> really strange here. I had no problems with the code above  
> > in earlier versions.
> > 
> > Thank you for your report. Please test the following patch.  
> > \>  
> > --- tcltklib.c.old 2004-06-04 10:15:14.000000000 +0900  
> > +++ tcltklib.c 2004-06-04 10:16:17.000000000 +0900  
> > @@ -1008,7 +1008,9 @@  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(VALUE)0);  
> > #else
> > 
> > + rb\_thread\_critical = Qfalse;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = rb\_eval\_string\_protect(arg-\>string, &status);  
> > + rb\_thread\_critical = Qtrue;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (status) {  
> > &nbsp;&nbsp;&nbsp;char \*errtype, \*buf;  
> > &nbsp;&nbsp;&nbsp;int errtype\_len, len;  
> > @@ -1288,9 +1290,13 @@  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct cmd\_body\_arg \*arg;  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE ret;  
> > + int thr\_crit\_bup;
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DUMP1("call ip\_ruby\_cmd\_core");  
> > + thr\_crit\_bup = rb\_thread\_critical;  
> > + rb\_thread\_critical = Qfalse;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = rb\_apply(arg-\>receiver, arg-\>method, arg-\>args);  
> > + rb\_thread\_critical = thr\_crit\_bup;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DUMP1("finish ip\_ruby\_cmd\_core");
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ret;  
> > \>  
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hidetoshi NAGAI  
> > (nagai@ai.kyutech.ac.jp)
