How to create a TimeSliceThread instance?

I'm using Introjucer 3.1.0.

I am trying to work through and understand the AudioDemoPlaybackPage module in the JUCE demo. I created a bare bones gui project (Main.cpp, MainComponent.h, and MainComponent.cpp), and am trying to expand that to play a soundfile.

What do I put in MainComponent.h and MainComponent.cpp to create a TimeSliceThread instance named 'thread' that I can eventually use in this line that I see in the JUCE AudioDemoPlaybackPage module:

transportSource.setSource (currentAudioFileSource, 32768, &thread, reader->sampleRate);

In MainComponent.h, I used what I see in AudioDemoPlaybackPage.h:

[...]
private:
[...]
    TimeSliceThread thread;
[...]

In the //[Constructor] part of MainComponent.cpp, I'm stuck. All my guesses are wrong.

What exactly do I say in MainComponent.h and MainComponent.cpp to create TimeSliceThread thread?

 

Do you initialize the TimeSliceThread in the constructor:

MainComponent::MainComponent()
: thread("SomeNameForTheTimeSliceThread")
{
// ...
}

That compiles. Thank you!

MainContentComponent::MainContentComponent()
    : thread("audio")
{
[...]
}

I thought I had tried that one a million times, copying from AudioDemoPlaybackPage.cpp, but obviously I hadn't done it correctly.

I am slowly learning, but I am still confused about TimeSliceThread.

So, referring to the code my first post, in MainComponent.h, I declared:

private:
[...]
TimeSliceThread thread;
[...]

So 'thread' is now "A thread that keeps a list of clients," as it says in the api.

Then, in
MainContentComponent::MainContentComponent(), I construct/inherit a 'client' called 'audio', and that's what  this means?

MainContentComponent::MainContentComponent()
    : thread("audio")

So 'audio' is now the first (and only) client in the list that 'thread' will keep track of after I say?:

MainContentComponent::MainContentComponent()
    : thread("audio")
{
[..]
thread.startThread (3);
[...]
}

 

The “: thread(“something”)” is the initialisation for the TimeSliceThread object. With a lot of classes you don’t need that, but some classes (like TimeSliceThread) have a no (or a private) standard constructor and you need to use a constructor with parameters.

For local use you would add this to the object declaration:

{
  TimeSliceThread thead("audio");
  // [...]
}

The initialisation list is the way to do the above with class members.

Thank you! That was very helpful. Your generous responses have been much appreciated!