Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make sure your delegate(iphone, cocoa) is thread safe?
    text
    copied!<p>I've seen a few examples of using delegate with multiple threads for async image loading with cocoa's URL loading system.</p> <p>I think the process goes like below.</p> <p>Thread 1 : give thread 2 an operation to perform, and a delegate which thread 2 will access upon completion.</p> <p>Thread 2 : upon completing the operation, calls a predefined selector on the delegate passed by thread 1.</p> <p>I'd like to implement similar feature(async processing with delegate) with my own classes. The below pseudo code is similar to the process outlined above, but i'd like to make it more detailed for the sake of discussion of thread safety.</p> <p>Thread 1 :<br> [begin critical section]<br> set delegate so that thread 2 can access<br> [end critical section]<br> fire an operation </p> <p>-(void) dealloc (of the delegate object's class) {<br> [begin critical section]<br> set delegate to null<br> [end critical section]<br> } </p> <p>Thread 2 :<br> actually works on operation<br> - upon completion.. [begin critical section]<br> get delegate<br> [delegate performSelectorOnMainThread @someSelector];<br> [end critical seciton] </p> <p>Now, I'm worried about the thread safety of accessing the delegate. In other words, <strong>how do the 2nd thread makes sure that the delegate is still valid when the delegate object can go away anytime without waiting for the operation completion</strong>.</p> <p>The pseudo code above is the best attempt I've made and wonder if it's gonna actually work. Furthermore, I've never seen any attempt on making the delegate thread safe in similar work such as <a href="http://www.markj.net/iphone-asynchronous-table-image/" rel="nofollow">http://www.markj.net/iphone-asynchronous-table-image/</a> Isn't the critical section safe guarding necessary? </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload