Skip navigation.
 
mlRe: How can I stop running loop?
FROM : Shawn Erickson
DATE : Tue Nov 23 23:09:36 2004

On Nov 23, 2004, at 1:48 PM, Mai Bui wrote:

> Hi,
>
> I'm very new in cocoa and still learning it.  I would like to write 
> application which is run forever (to acquire some data) until the user 
> enter some key (i.e "Q") and application will stop.
> I think about use multithread to do it: the first thread runs a 
> while(1) loop, the second one waits, checks and gets input from the 
> user to set the flag "Done" which is the  first thread can see it and 
> stop.  My logic is on the right track?  But .... I do not know how to 
> implement with the second thread.
>
> Thanks for your ideas, your guide in advance.


A few questions...

What are you gathering data from?
Can you block waiting for data or do you need to poll the source of 
data?

In Cocoa your main thread (the first thread) is setup by NSApplication 
to run an NSRunloop that is hooked up to handle using input sources 
(user generated events like key presses, mouse clicks, etc.). This 
would be the thread that you would use to detect user input... to 
decide if you should stop (and presumably start) collecting data.

Depending on the source of the data that you are collecting the main 
thread could also gather the data (feed the data into the runloop it 
runs via events of some type or potentially by a timer firing that 
triggers you to go poll for more data periodically, etc...).

Anyway it is likely best to use a secondary thread to gather your data 
as you generally outlined. This secondary thread would loop collecting 
data (ideally blocking when waiting for more data) and while doing so 
would periodically check to see if should stop collecting based on an 
message, boolean flag, etc.

Review the documentation on NSThread for more information...

<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSThread.html
>

...and conceptual documentation about threading in Cocoa...

<http://developer.apple.com/documentation/Cocoa/Conceptual/
Multithreading/index.html
>

-Shawn

Related mailsAuthorDate
mlHow can I stop running loop? Mai Bui Nov 23, 22:48
mlRe: How can I stop running loop? John Stiles Nov 23, 22:52
mlRe: How can I stop running loop? Shawn Erickson Nov 23, 23:09
mlRe: How can I stop running loop? Mai Bui Dec 2, 05:48
mlRe: How can I stop running loop? Shawn Erickson Dec 2, 17:23