Safely terminating a thread, or the dealloc that doesn't dealloc?
Safely terminating a thread, or the dealloc that doesn't dealloc?
- Subject: Safely terminating a thread, or the dealloc that doesn't dealloc?
- From: Glen Low <email@hidden>
- Date: Mon, 15 Mar 2004 21:13:41 +0800
Hi all,
In my kqueue implementation for Graphviz, I have an object that spawns
a thread. The thread then occasionally notifies the object of
interesting events via performSelectorOnMainThread. The object can also
pass messages to the thread via a pipe. Now in order to perform proper
termination, the object should send a "kill me" message to the thread.
So far so good.
Except...
If the object sends the "kill me" message to the thread during its
dealloc, there is a small chance that the thread will still have other
pending messages to send to the object, in which case it will crash. In
essence, I need to extend the lifetime of the object to the lifetime of
the thread.
Some thoughts:
1. In my object dealloc, send the message but don't call super dealloc.
Arrange for the thread to call the object one last time, and the last
method does the real dealloc. Not quite Cocoa-kosher to me.
2. The thread could retain the object and release it upon its end --
but I doubt retain and release are threadsafe.
3. On dealloc, wait for the thread to terminate. I don't know if you
can "join" a thread in Cocoa, but I don't want to incur the overhead of
a blocking call.
4. Something else?
Cheers, Glen Low
---
pixelglow software | simply brilliant stuff
www.pixelglow.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.