Re: Sending image from separate thread back to AppController
Re: Sending image from separate thread back to AppController
- Subject: Re: Sending image from separate thread back to AppController
- From: j o a r <email@hidden>
- Date: Wed, 21 Jul 2004 01:59:14 +0200
Contention for UI objects is not the only thing that could cause
problems in a multi threaded environment. While I can't say for sure
that what you're doing is wrong, I don't think it's safe to assume that
any object or method can be called from a secondary, or non-main,
thread unless it's made absolutely clear from their documentation. As
the available documentation doesn't mention NSMatrix or NSCell to be
thread safe, I would assume that they're not thread safe.
The docs say that:
"Immutable objects are generally thread-safe. Once you create them, you
can safely pass these objects to and from threads. On the other hand,
mutable objects are not thread-safe. To use mutable objects in a
threaded application, the application must synchronize appropriately."
<
http://developer.apple.com/documentation/Cocoa/Conceptual/
Multithreading/Concepts/safety.html>
Matrixes and cells are mutable objects, right? So while you should
probably lock access to the matrix, it's probably easier to funnel the
generated image to the main thread and update the matrix there.
I could be wrong, and would appreciate opinions and input from other
list members.
j o a r
On 2004-07-21, at 01.07, John Pannell wrote:
>
I'm not sure I could call anything I'm doing "best practice", but I
>
have an app that generates an image in a thread and then places the
>
image in the UI (in a cell in a matrix, to be specific). The method is
>
part of my AppController, and is spun off into a separate thread via
>
>
[NSThread detachNewThreadSelector:@selector(addImageWithURL:)
>
toTarget:self withObject:myURLString];
>
>
Now, with this usage, the thread does know about the outlets to various
>
UI elements that the AppController knows about. So, in the method
>
running in the separate thread, I call
>
>
[[thePreviewView cellAtRow:row column:column] setImage:thumbImage];
>
>
where thePreviewView is an IBOutlet in my AppController's header file.
>
It all seems to work without a hitch. The thread performs a
>
calculation so that every thread is working on a unique row/column, so
>
there is no thread contention for UI objects.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.