View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] setExitHandler & threads

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Sun, 13 Aug 2006 17:40:08 +0200

Scott Gargash schrieb:
>
> I've run into an interaction with threads and the XOTcl exit handler
> that I don't understand.
>
> I create a thread from within an object, and release that thread in
> the object's destructor. If I destroy the object directly,
> everything's fine, but if I destroy the object through "Object
> setExitHandler", I get an error. Am I treading into undefined
> behavior? Is there well-defined way to shut down multiple threads?
>
the thread and exit code is a tricky and sensitive area, especially if
it is supposed to work with tcl and the aolserver. below is the somewhat
simplified code (btw., your example works on mac os x without a
problem). In general. there is no need to destroy the objects by hand,
since these are destroyed automatically and the appropriate destroy
methods are executed. also, the destroy order is not completely trivial
in order to be able to execute the destroy methods correctly. i would
not do this by hand.

isn't the default beavior sufficient? it prints:

::a created
thread waits
exithandler
::a destroyed

-gustaf


package require XOTcl
package require Thread

namespace import {xotcl::*}
Class A
A instproc init {} {
  puts stderr "[self] created"
  my set tid [thread::create [subst -nocommands {
    set ::auto_path [list $::auto_path]
    package require XOTcl
    namespace import {xotcl::*}
    Object b
    puts stderr "thread waits"
    thread::wait
  }]]
}

A instproc destroy {args} {
  puts stderr "[self] destroyed"
  thread::send [my set tid] {b destroy}
  thread::release [my set tid]
  next
}

A a
xotcl::Object setExitHandler {puts stderr exithandler}