On Oct 28, 2010, at 9:16 AM, Jonny Taylor wrote:
>> Note that sending notifications to other threads while holding locks in the 
>> current thread (or queue) is rife with danger & fragility.  It is great way 
>> to create deadlocks and, if not, to end up with a solution that has all the 
>> maintenance costs of multi-threaded code combined with all the performance 
>> wins of single threaded execution....
> My situation is that I am receiving camera frames on a background thread, and 
> need a way of informing various windows that their displays should be updated 
> to reflect the fact that a new frame has been received. This GUI work needs 
> to be done on the main thread (I believe...). In addition, I take advantage 
> of the coalescing features of notifications to avoid unnecessary updates. 
> Thus I am keeping the currentFrame variable up to date in a threadsafe 
> manner, and then after updating it (and releasing the lock) I post a 
> notification to the main queue, with coalescing. Does that sound like a 
> reasonable approach?
> 
> I definitely need to access currentFrame in a threadsafe manner, but 
> hopefully my description shows that the multithreaded relationship for the 
> notifications and variable updates is not there for performance reasons, if 
> that makes sense. I am more than aware of the trouble multithreading brings, 
> but the nature of the code makes it pretty unavoidable.

The best way to access something in a threadsafe manner is to only access it 
from one thread at a time without locking.

And the best way to do that is to perpetuate the notion of thread ownership.   
When thread A is accessing the object, thread B not only does not access the 
object, thread B doesn't even have a reference to the object in its scope of 
execution.  Not ever.

When B -- likely the main thread -- needs to access the object, then A passes 
ownership to B, typically via GCD, operations or -performSelectorOnMainThread:

b.bum

_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [email protected]

Reply via email to