Re: NSTimer and user interface events
Re: NSTimer and user interface events
- Subject: Re: NSTimer and user interface events
- From: Shawn Erickson <email@hidden>
- Date: Mon, 12 Jan 2004 08:37:26 -0800
On Jan 12, 2004, at 8:12 AM, Shoaib wrote:
I have an NSTimer that fires every 5 seconds and updates a data
structure in my application. The same data structure can also be
updated through a user interface event (like the user editing the
data).
My question is the following:
Do I need to protect my data structure from simultaneous updates from
the timer and user interface events?
Short answer: No, unless you have created secondary threads.
Longer answer:
Your application has a main thread that runs what is called a run loop
(NSRunLoop). This run loop receives events from various sources like
the OS (mouse, keyboard, etc.), from timers (NSTimer), from
notifications (NSNotification), etc.. When an event is received by the
run loop it dispatches it to the appropriate "handler". Run loops
basically sequentially processes all events it receives.
So you will not have concurrent updates to your data structures.
Now if you create secondary threads (which can run their own run loops)
that also touch those structures you may have issues depending on what
they do (if you can make such that only one thread actually writes but
all other read you should be able to avoid locking).
I would review the docs on NSTimers, NSRunLoop and NSThread.
-Shawn
_______________________________________________
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.