How can I destroy my instance?

Writing and using Classes in Dyalog APL
Post Reply
User avatar
Budgie
Posts: 36
Joined: Thu Nov 26, 2009 9:22 am
Location: Beckenham

How can I destroy my instance?

Post by Budgie »

I obviously don't know what I'm doing, and I'm doing something horribly wrong here, because what I want to do won't work.

I'm trying to set up a server class, using a Listen function as the constructor, and this function calls a Wait function that loops (until boolean done is set to 1) in a new thread.

When I want to shut down the server, obviously I need to do both of these:
[*] set done←1, which will stop the endless loop, and the thread will shut itself down naturally (I'd rather not ⎕TKILL the thread if I can help it)
[*] erase the class instance

I tried coding to set done←1 in the destructor, but on saying )erase xyz nothing happens because the destructor doesn't get called even though xyz is erased, because the instance carries on operating. My other thought is to get the instance to erase itself after the thread has shut down, but I can't figure out how to do that.

Ideally, I'd like to use )erase or ⎕EX to shut the server down and erase the instance. Has anyone got any thoughts on this?
Jane
Erik.Friis
Posts: 66
Joined: Mon Apr 04, 2011 3:16 pm

Re: How can I destroy my instance?

Post by Erik.Friis »

Jane,

:class Foo
{del}Foo
:implements constructor
:access public

{comment} kick off the new thread here...
{del

{del}NoFoo
:implements destructor
:access private

MyThread.Done<-1
{del}
:endclass

MyFoo<-[]NEW #.Foo

You can kick off the thread in MyFoo's constructor.

When you ")erase MyFoo" the destructor method Foo will be called and you can set Done to 1 in the object running in a thread to terminate the thread. Not sure why your destructor is not getting called -- worked for me here...
User avatar
Budgie
Posts: 36
Joined: Thu Nov 26, 2009 9:22 am
Location: Beckenham

Re: How can I destroy my instance?

Post by Budgie »

Erik

I think it's because the function running in the new thread is from the class, and that needs to finish before the destructor will start.

According to the documentation an instance only disappears when the last reference to it disappears. So my ")erase xyz" erases the visible reference to the class, but an invisible reference is left, because the function from the class is still running. Only when that function terminates does the instance disappear and the destructor is called.
Jane
Erik.Friis
Posts: 66
Joined: Mon Apr 04, 2011 3:16 pm

Re: How can I destroy my instance?

Post by Erik.Friis »

Not sure why they consider the thread as holding a separate reference to the object -- this might be a bug. I guess you can define a property to shut down the thread and sit in a wait loop till it terminates. Then you can do your )erase.
Post Reply