Gabriel M. Beddingfield wrote:
> Very close! The problem appears to be that Hydrogen::get_instance() is
> called
> at nearly the same time by two threads in the startup code (one has to do
> with
> opening the first song file, the other for the regular startup code).
What version did you test it with? The #37 fix has been applied _after_
beta3 has been released. So, in theory, the bug shouldn't happen with
trunk anyway, at least if my fix is correct (the trunk contains only one
of my two patches, it should be good enough but I'd have to double-check
to be sure). Of course, chances are that my fix isn't entirely correct :)
If it still happens *with trunk*, then just adding a mutex might improve
the thread safety while worsening realtime safety, by causing priority
inversion once in a while (low-priority GUI thread may block the audio
thread for too long).
You can have both thread-safety and realtime-safety (+efficiency) using
this:
Hydrogen* Hydrogen::get_instance()
{
if ( __instance == NULL) {
static QMutex _mx;
QMutexLocker lk(&_mx);
if ( __instance == NULL ) {
__instance = new Hydrogen();
}
}
return __instance;
}
http://www.cs.wustl.edu/~schmidt/editorial-3.html
It still wouldn't work if two threads called get_instance() at the same
time, because the _mx object might be constructed two times
(initialization of local static variables isn't guaranteed to be
thread-safe, as far as I know). But that won't happen in this particular
case: the extra threads don't exist before the first call to Hydrogen
constructor.
> While the Hydrogen() constructor is executing... someone else calls
> get_instance() and, finding a NULL __instance, double-calls the constructor.
That "someone else" is the MIDI thread code. Again, see #37 and related
discussion, it has almost too much of a detailed explanation.
> Below is a fix for the problem at hand. Please see that it fixes it for you.
> I'll post a complete solution (for all *::get_instance() accessors) during my
> lunch break tomorrow.
See above - it might be better to call the accessors on program
initialization before extra worker threads are created, as it removes
the need for accessor mutexes completely. Why use a complicated solution
where a simple one is safer and more efficient? :)
Krzysztof
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Hydrogen-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hydrogen-devel