Re: NSTimer not working in a multithreaded application
Re: NSTimer not working in a multithreaded application
- Subject: Re: NSTimer not working in a multithreaded application
- From: Greg Guerin <email@hidden>
- Date: Fri, 3 Sep 2010 00:02:00 -0700
Abhijeet Singh wrote:
Hi,I am working on a multithreaded software that runs on a medical
instrument. The software has 2 parts. GUI and worker threads
(worker threads sends commands to instrument). The GUI is developed
using ObjectiveC and Cocoa. Worker threads are all in C and
Carbon.It is a Cocoa application.I am working on GUI of the
software.When the application starts it first creates worker
threads ( that initializes the hardware/instrument). Once the
threads are created one of the thread sends a message back to main
thread. My problem isi need to start a timer when the main thread
receives a message from worker thread. I am starting a timer as
follows:timer = [[NSTimer scheduledTimerWithTimeInterval:10.0
target:self selector:@selector(turnOffLight:) userInfo:nil
repeats:NO] retain];But it does not work. I debugged it and found
that the timer is created but "turnOffLight" is never executed.Then
I created the timer just before worker threads creation and it worked.
You may have been the victim of Cocoa's builtin multithreading locks,
which are not initialized until multithreaded mode is entered.
See the heading "Using Posix Threads in a Cocoa Application", sub-
heading "Protecting the Cocoa Frameworks", in the Thread Management
section of the Threading Programming Guide.
--begin-quote--
For multithreaded applications, Cocoa frameworks use locks and other
forms of internal synchronization to ensure they behave correctly. To
prevent these locks from degrading performance in the single-threaded
case, however, Cocoa does not create them until the application
spawns its first new thread using the NSThread class. If you spawn
threads using only POSIX thread routines, Cocoa does not receive the
notifications it needs to know that your application is now
multithreaded. When that happens, operations involving the Cocoa
frameworks may destabilize or crash your application.
To let Cocoa know that you intend to use multiple threads, all you
have to do is spawn a single thread using the NSThread class and let
that thread immediately exit. Your thread entry point need not do
anything. Just the act of spawning a thread using NSThread is enough
to ensure that the locks needed by the Cocoa frameworks are put in
place.
If you are not sure if Cocoa thinks your application is multithreaded
or not, you can use the isMultiThreaded method of NSThread to check.
--end-quote--
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/
Multithreading/CreatingThreads/CreatingThreads.html
Since you're mixing Cocoa and Carbon, you should read that entire
section on Thread Management, and probably the entire Thread
Management Guide.
-- GG
_______________________________________________
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