Hello I have an application that has a shared object (which is a custom container for elements - arrays, and which is protected by critical sections in my code). I need to control it's lifetime - it should be alive until the last thread finishes working. Let me try to explain it using an example from my code.
I have a display link (a method, that is periodically called by a CoreVideo framework-spawned "high priority thread"): -(void)displayLinkCallbackMethod { do_something_with_the_object(myObject); spawn_thread_for_selector(mythreadfunction); do_something_with_the_object(myObject); spawn_thread_for_selector(mythreadfunction); do_something_with_the_object(myObject); } -(void)mythreadfunction() { do_something_with_the_object(myObject); } -(IBAction)button1_clicked { myObject = [[MyObject alloc] init]; DisplayLinkStartThread(displayLinkCallbackMethod); } -(IBAction)button2_clicked { DisplayLinkStopThread(); //[myObject release]; //can't do it here, it's wrong } The main thread of my application starts the Display Link thread (which starts calling displayLinkCallbackMethod as fast as possible). Now, when i stop Display Link thread, the unfinished part of "displayLinkCallbackMethod" is being finished, and the method is never called again. The threads, spawned by this callbackmethod, finish executing too. And of course, these "mythreadfunction" threads need myObject to be alive for some time, i can't deallocate the object right after i stop display link thread. However, i need to deallocate it sooner or later, as long as it consumes a big chunk of memory. Reference counters would come very handy here, but i am not sure if this is thread safe: yes, i can retain myObject when i enter displayLinkCallbackMethod() and mythreadfunction() (and release this object when i quit these functions). Then i just release the object right after stopping displaylink, and the object is either deallocated immediately (if its refcount was 1), or it is deallocated when the last thread that was using it finishes. It would look somehow like the following (very schematically): -(void)displayLinkCallbackMethod { [myObject retain]; do_something_with_the_object(myObject); spawn_thread_for_selector(mythreadfunction); do_something_with_the_object(myObject); spawn_thread_for_selector(mythreadfunction); do_something_with_the_object(myObject); [myObject release]; } -(void)mythreadfunction() { [myObject retain]; do_something_with_the_object(myObject); [myObject release]; } -(IBAction)button1_clicked { myObject = [[MyObject alloc] init]; DisplayLinkStartThread(displayLinkCallbackMethod); } -(IBAction)button2_clicked { DisplayLinkStopThread(); [myObject release]; //kind of safe now } The problem arises when i stop display link, and the display link at the moment when displaylinkcallback() just started, but haven't actually executed my first [myObject retain] - here the myObject is deallocated, and displayLinkcallback() works with "nil". I haven't faced this on 2CPUd mac, but i can't be sure it never happens. Could you please suggest me how to achieve the removal of the object when i need it? In the most "pretty" way, if one exists. Thanks for reading this long post :) _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com