# Ruby Tk and Threads error

**URL:** <https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808>\
**Category:** Ruby\
**Created:** [November 30, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808 "2006-11-30T18:10:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorge\_santiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jorge\_santiago](https://www.ruby-forum.com/u/jorge_santiago)\
**Post date:** [November 30, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808/1 "2006-11-30T18:10:28Z")

</div>

Hi,

I’m coding an application with Ruby and RubyTk that draws a huge  
amount of lines on a TkcCanvas. I split the drawing code in a new  
thread so the main interface remains responsive to the user to cancel  
the operation at any time.

The problem is when I click the “stop drawing” button which is bound  
to a “proc { @drawing\_thread.exit }” I _often_ (not always) get the  
following error:

/usr/local/rubytk/lib/ruby/1.8/tk.rb:1537:in `mainloop': killed thread (ThreadError) from /usr/local/rubytk/lib/ruby/1.8/tk.rb:1537:in`mainloop’  
from plfg.rb:250:in `initialize’  
from plfg.rb:841

I’m on Ubuntu Edgy 6.10 with ruby-1.8.4 compiled from source with  
–enable-pthread

I believe it has something to do with some system library or the  
compilation flags, because in windows using the one click installer  
and the activetcl 8.4 the application never seems to crash like that.

Thanks in advance for your help,

jorge santiago.

---

<div class="post-metadata">

**Author:** ![jorge\_santiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jorge\_santiago](https://www.ruby-forum.com/u/jorge_santiago)\
**Post date:** [December 1, 2006, 8:24am UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808/2 "2006-12-01T08:24:54Z")

</div>

From: “jorge santiago” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Ruby Tk and Threads error  
Date: Fri, 1 Dec 2006 02:08:56 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> The problem is when I click the “stop drawing” button which is bound  
> to a “proc { @drawing\_thread.exit }” I _often_ (not always) get the  
> following error:
> 
> /usr/local/rubytk/lib/ruby/1.8/tk.rb:1537:in `mainloop’: killed thread  
> (ThreadError)

Probably, that is a bug which depends on timing of callback operation.  
Please try the following patch.

# Index: ext/tk/tcltklib.c

RCS file: /var/cvs/src/ruby/ext/tk/tcltklib.c,v  
retrieving revision 1.40.2.13  
diff -u -r1.40.2.13 tcltklib.c  
— ext/tk/tcltklib.c 10 Jul 2006 09:51:30 -0000 1.40.2.13  
+++ ext/tk/tcltklib.c 1 Dec 2006 07:20:02 -0000  
@@ -4,7 +4,7 @@

- 

```
         Oct. 24, 1997 Y. Matsumoto

```

\*/

-#define TCLTKLIB\_RELEASE\_DATE “2006-07-10”  
+#define TCLTKLIB\_RELEASE\_DATE “2006-12-01”

#include “ruby.h”  
#include “rubysig.h”  
@@ -5526,10 +5526,15 @@  
\*(q-\>done) = -1;

```
 /* back to caller */

```

- DUMP2(“back to caller (caller thread:%lx)”, q-\>thread);
- DUMP2(" (current thread:%lx)", rb\_thread\_current());
- rb\_thread\_run(q-\>thread);
- DUMP1(“finish back to caller”);

- if (RTEST(rb\_funcall(q-\>thread, ID\_alive\_p, 0, 0))) {
- 

```
 DUMP2("back to caller (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- 

```
 rb_thread_run(q->thread);

```

- 

```
 DUMP1("finish back to caller");

```

- } else {
- 

```
 DUMP2("caller is dead (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- }

- DUMP2(“back to caller (caller thread:%lx)”, q-\>thread);
- DUMP2(" (current thread:%lx)", rb\_thread\_current());
- rb\_thread\_run(q-\>thread);
- DUMP1(“finish back to caller”);

- if (RTEST(rb\_funcall(q-\>thread, ID\_alive\_p, 0, 0))) {
- 

```
 DUMP2("back to caller (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- 

```
 rb_thread_run(q->thread);

```

- 

```
 DUMP1("finish back to caller");

```

- } else {
- 

```
 DUMP2("caller is dead (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- }

- DUMP2(“back to caller (caller thread:%lx)”, q-\>thread);
- DUMP2(" (current thread:%lx)", rb\_thread\_current());
- rb\_thread\_run(q-\>thread);
- DUMP1(“finish back to caller”);

- if (RTEST(rb\_funcall(q-\>thread, ID\_alive\_p, 0, 0))) {
- 

```
 DUMP2("back to caller (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- 

```
 rb_thread_run(q->thread);

```

- 

```
 DUMP1("finish back to caller");

```

- } else {
- 

```
 DUMP2("caller is dead (caller thread:%lx)", q->thread);

```

- 

```
 DUMP2(" (current thread:%lx)", 

```

rb\_thread\_current());

- }

---

<div class="post-metadata">

**Author:** ![jorge\_santiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jorge\_santiago](https://www.ruby-forum.com/u/jorge_santiago)\
**Post date:** [December 4, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808/3 "2006-12-04T20:42:44Z")

</div>

Hi Hidetoshi,

Your patch worked GREAT! My app has never crashed since I applied the  
patch.

Thanks a lot!

jorge santiago.

---

<div class="post-metadata">

**Author:** ![jorge\_santiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jorge\_santiago](https://www.ruby-forum.com/u/jorge_santiago)\
**Post date:** [December 2, 2006, 11:15am UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808/4 "2006-12-02T11:15:05Z")

</div>

Jorge,

I’m also on Ubuntu Edgy and I earlier compiled Ruby with the pthreads  
option, and it gave me a warning that unless Tcl/Tk is also compiled  
with pthreads, you will have problems like segfaults. I then rebuilt  
without pthreads and I have no issues with it. The Tcl/Tk that is  
standard for the Ubuntu/Debian distro is apparently (from what I saw)  
NOT compiled with pthreads, so try compiling Ruby without pthreads and  
see if that helps. I believe you don’t need pthreads unless Tcl/Tk is  
compiled with it.

---

<div class="post-metadata">

**Author:** ![jorge\_santiago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jorge\_santiago](https://www.ruby-forum.com/u/jorge_santiago)\
**Post date:** [December 5, 2006, 2:47am UTC](https://www.ruby-forum.com/t/ruby-tk-and-threads-error/77808/5 "2006-12-05T02:47:59Z")

</div>

From: “jorge santiago” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby Tk and Threads error  
Date: Tue, 5 Dec 2006 04:40:39 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Your patch worked GREAT! My app has never crashed since I applied the patch.  
> Thanks a lot!

I have to say thank you.  
Bug reports and requests from users improve Ruby/Tk. 🙂  
Thank you for your report.
