Re: Leak while in Midi thread
Re: Leak while in Midi thread
- Subject: Re: Leak while in Midi thread
- From: Lubor Prikryl <email@hidden>
- Date: Tue, 21 Jan 2003 18:51:43 -0800
Hi,
What do you mean to be the best way to get MIDI data from its thread in
Carbon? I have double buffered these data and read by a function,
called by (not idle, but still low priority) EventLoopTimer. But Carbon
I/O thread has sometimes too many things to do to let the timer make
its work. Is there a better solution - opening a special new thread?
Which API to use in this case? I also tried to send an event (created
outside the MIDI thread), but it blocks event queue.
Thanks for a hint
Lubor, DSound, www.dsound1.com
On Saturday, January 11, 2003, at 12:18 PM, Jeff Moore wrote:
The Cocoa documentation has lots of warnings about making AppKit calls
from threads other than the main thread (Foundation is marginally
better, but not much, you still need autorelease pools for most of
those APIs), so you are on shaky ground to begin with.
It all comes down to being cognizant of the environment you are
operating in. Threads like those used for MIDI callbacks and for the
HAL's IO cycle are more functionally equivalent to hardware interrupts
than to normal user threads and really need to be treated as such.
Just because you can call malloc() anytime, anywhere doesn't mean that
you should.
Instead, you should be pre-allocating all the memory you plan on
touching in those threads and making use of things like command queues
to convey information from these threads to the rest of your program.
Also, you need to avoid doing things that might block the thread (the
HAL is especially sensitive to this) like locking a mutex. Further,
you have to avoid constructs like spin locks that rely on thread
priorities degrading when the thread becomes CPU bound (a spin lock in
a time constraint thread is the easiest way to lock your system up).
Hope this helps a bit. You can find out lots more from any good book
on real time systems programming.
On Saturday, January 11, 2003, at 01:38 AM, Craig Bakalian wrote:
On Friday, January 10, 2003, at 09:58 PM,
email@hidden wrote:
Subject: Re: Leak while in Midi thread
From: Jeff Moore <email@hidden>
To: CoreAudio <email@hidden>
This discussion seems like as good a time as any to remind people
that
allocating and deallocating memory inside a time constraint thread
such
as this MIDI thread or the HAL's IO thead is flat out bad
programming.
The reason why is that the memory calls _will_ block (usually
precisely
when you don't want them to) and cause you timing problems that are
very hard to diagnose. This is among the reasons why there are no
autorelease pools in these threads.
To reiterate: don't allocate or deallocate memory in a time
constraint
thread owned by the audio or MIDI systems. You will get hurt by doing
so.
So how do we get around this? I am not having timing problems
because my app is just getting the tone value or midi value of each
midi key press and I am using the numeric key pad to enter the
rhythmic value of a given music note. But strange things happen
sometimes. Some times the tone value does not change. What do we do
when we are using Cocoa?
--
Jeff Moore
Core Audio
Apple
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.