Re: interthread communication
Re: interthread communication
- Subject: Re: interthread communication
- From: Mike Shields <email@hidden>
- Date: Fri, 13 Jul 2001 18:28:42 -0600
From going from a thread to the main thread to update progress, I think
there are only a few ways to 100% successfully do it. DO, Omni's
OFMEssageQueue stuff and possibly NSDistributedNotifications (I don't
know much about them). As for non UI information passing (like
reading/writing from a queue) yes, NSLocks (or even pthread_mutex's)
work just dandy and you can use all the normal coding practices you know
and love.
As for why to change it? Well if it's working right now is no indication
it'll work ever again. I had a bug in the last bit of threaded code I
was writing which worked fine on my machine for a month, it got to the
testers and started failing. All that had to be done was a trivial
change in usage patterns and BAM! I hit a race condition which caused
the thread to step on another thread's access.
Do it right rather than do it again. (Yeah like your boss will actually
allow that :^)
Mike
PS: Is there really a need to pass data between the work thread and UI
50 times/second?? I'd suspect that more like 2-10 is a better use of
processing power depending. Of course you might have a reason, but
unless you're updating the screen 50 times/second it seems a little
excessive
On Friday, July 13, 2001, at 05:52 PM, Andreas Wolf wrote:
what is the recommended way of communicating between threads? I'm
writing an
internet client application, and I have all the protocol and networking
stuff in one thread. how can I safely send messages to a controller for
instance to update the UI?
right now, I'm just posting notifications to the app's defaultCenter
and
letting the controller catch them and perform operations on the UI. is
this
acceptable?
tia,
dan
I've tried Distributed Objects, NSPorts and their messsages,
NSDistributedNotifications and I think a couple more..
but the only thing that really worked for me, when sending frequent
(about 50 per second) messages between
a working thread and a UI thread was using basic C types (C-strings,
integers,..) and
NSLock's, i.e. whenever you access any shared value you lock them, to
prevent the other thread from
accessing that same value at the same time.
NSStrings and others didn't seem to be thread save although the cocoa
release notes
says so. Distributed Objects, NSPortMessages were too slow and
everything else I've tried
(even sample code posted on this list and other lists to date) didn't
work for me either.
I am surprised that your solution with notifications to the
defaultCenter works. When I tried
this the thread that sent the notification was also the receiving
thread (no inter-thread communication).
However, like John said, if it works its fine... why change it? :)
-A
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev