Re: Threadsafe function help
Re: Threadsafe function help
- Subject: Re: Threadsafe function help
- From: Michael Ash <email@hidden>
- Date: Fri, 1 May 2009 00:59:39 -0400
On Fri, May 1, 2009 at 12:50 AM, Ken Tozier <email@hidden> wrote:
> Hi
>
> I'm trying to write a general purpose console-like view and have most of the
> parts working except the convenience functions for adding attributed strings
> to an NSTextView subclass. Basically, what I want is to have a couple of
> functions that can be used like NSLog, but am finding that my functions seem
> to halt execution when they a re called from inside threads other than the
> main thread. I'm using
> "performSelectorOnMainThread:withObject:waitUntilDone:" to append the text
> to the NSTextView but this doesn't seem to be enough. Something I'm doing is
> causing a block somewhere when more than one thread tries to write to the
> NSTextView. If only one thread is created then my log function seems to
> work, but when two or more threads try to use the log functions, it locks
> up.
>
> For the basic threading, I'm using NSOperationQueues and
> NSInvocationOperations and everything runs as expected when I use NSLog, but
> when I try to call my KCLog function, execution stops.
>
> Could someone point out what I'm doing wrong in my log functions?
"It locks up" is not a very useful description. Use the debugger and
find out *where* it locks up.
I'll wager it's due to your manipulation of GUI objects in that last
method you posted, though. GUI objects can *only* be manipulated from
the main thread. You apparently know about this, because you dump your
append calls onto the main thread using performSelectorOnMainThread:,
but you fail to do this for a bunch of other calls, like length and
scrollRangeToVisible:.
That entire method should really be on the main thread. Don't bother
trying to bump individual calls to the GUI objects onto the main
thread. At best you'll end up with tremendously confusing and somewhat
inefficient code. At worst you'll end up with weird race conditions
resulting from interleaving calls on different threads. Write a nice
easy method that does the update on the main thread, and then invoke
that whole method using performSelectorOnMainThread:.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden