# Audiothumbnail and thread safety

**URL:** <https://forum.juce.com/t/audiothumbnail-and-thread-safety/3995>\
**Category:** General JUCE discussion\
**Created:** [June 10, 2009, 7:08pm UTC](https://forum.juce.com/t/audiothumbnail-and-thread-safety/3995 "2009-06-10T19:08:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AaronLeese](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aaronleese/32/624_2.png) [@AaronLeese](https://forum.juce.com/u/AaronLeese)\
**Post date:** [June 10, 2009, 7:08pm UTC](https://forum.juce.com/t/audiothumbnail-and-thread-safety/3995/1 "2009-06-10T19:08:23Z")

</div>

Hi -

I’ve been experimenting with the juce QTAudioFormat that allows MP3 decoding.

Since the decoder is rather slow, I load the file in a background worker thread so that I can keep the program UI and other functions moving along and start playing the file immediately.

I also took a look at the audioThumbnail class … awesome. I love that I can just start it with a few simple commands and it will create the thumbnail in (another) background thread.

Thread safety? … ohhhh, you see where this is going. Two background threads trying to access the same resource can be bad news … and is at least with MP3’s.

I’m pretty familiar with thread safety issues, but am not sure how the audiothumbnail class works exactly … or if I have access to that thread? I tried creating a criticalSection object … but since I have no way to tell the audiothumbnail thread to use it, I’m at a loss.

Is there a way to lock a resource in juce … as in lock(file) … so that only one thread will access a file, or buffer, etc. … at a time? Or can I sync the audiothumbnail thread … use threadpool or timeslice to only trigger the thread as the file reader thread exits, and vice versa … ?

I feel there is an easy answer here and I just haven’t found it in the source tree yet (juce seems to have built in solutions for every problem when I look long enough).

Anyway, any help is appreciated.

Thanks  
Aaron

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 11, 2009, 8:45am UTC](https://forum.juce.com/t/audiothumbnail-and-thread-safety/3995/2 "2009-06-11T08:45:38Z")

</div>

You’d really need two different reader objects for this to work.

Even if a reader was thread-safe, you couldn’t let two things read it at the same time - it’d be jumping about and rebuffering so much that the performance would be vastly slower than just using two separate streams.

---

<div class="post-metadata">

**Author:** ![AaronLeese](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aaronleese/32/624_2.png) [@AaronLeese](https://forum.juce.com/u/AaronLeese)\
**Post date:** [June 11, 2009, 5:34pm UTC](https://forum.juce.com/t/audiothumbnail-and-thread-safety/3995/3 "2009-06-11T17:34:04Z")

</div>

Hmmm … I kinda thought I already had two separate reader objects … or at least two separate streams. Here’s what I’m doing …

… instantiate the thumb

audioThumb = new AudioThumbnail(256, \*AudioFormatManager::getInstance(), \*audioThumbCache);

… then after the load button is clicked …

// first, create this reader …  
reader = AudioFormatManager::getInstance()-\>createReaderFor(\*fileToLoad);

// now with the same file … an audiothumbnail object  
audioThumb-\>setSource(new FileInputSource (\*fileToLoad));

… so there are two readers here … both off the same AudioFormatManager instance … right? Or does the manager see the same file path and try to use just one reader ?
