Threads, messages and blocking
Threads, messages and blocking
- Subject: Threads, messages and blocking
- From: Sean Harding <email@hidden>
- Date: Thu, 29 May 2003 13:27:05 -0700
I think I'm missing something, or I'm taking the wrong approach here.
I apologize for my ignorance of multithreaded programming; I've managed to
mostly avoid it except in very basic POSIX threads apps for years. I've
found a lot of examples on the web and in Apple's developer docs that get at
parts of the problem I'm working on, but it's still not clicking.
I have an Objective-C Cocoa application with two threads (NSThread). I need
thread 1 to tell thread 2 to do something and to know when thread 2 is done
doing it. But thread 1 must not block while thread 2 carries out its work
(if blocking was ok, I wouldn't even need a second thread). It seems that
setting up an NSConnection between the threads (as demonstrated in the Apple
"Forming Connections Between Threads" document) won't solve my problem
because thread 1 will send the message to thread 2 and then block while the
work is carried out (until the called method returns). Right?
I looked at NSDistributedNotificationCenter as a possible alternative.
Sending notifications between the objects would at least get rid of
the blocking problem. But it's not clear to me whether that is the
right way to go or if it will even work in the first place. The docs
say that NSDistributedNotificationCenter should not be used to communicate
between threads "within the same task." I'm not clear on what the
definition of "task" in this context. Does that mean the same "process"?
I don't think it's practical to start a new thread for the each bit of
work and then know that it's done when the thread exits because sometimes
there are several short (1 second or less) tasks to do in a small period of
time. While my app isn't CPU-bound, constantly creating new short-lived
threads seems like a bad idea.
What's the most correct approach to solving this problem in OS X? I have a
feeling I'm missing something obvious. This can't be a unique problem.
Thanks.
sean
--
Sean Harding -- email@hidden --
http://dogcow.org/s/ -- KD7UAY
Seattle, WA, USA
_______________________________________________
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.