Implementing Juce Threads?

Hello everyone,
I’ve been trying to create a Juce Thread in an application, to try and get a better throughput. However I keep getting a Linker error. It specifically says:

"Undefined symbols for architecture x86_64:
“MainComponent::myThread::myThread(juce::String const&, unsigned long)”, referenced from:
MainComponent::MainComponent() in MainComponent.o

I’ve been trying to create a “myThread” class in MainComponent.h, which inherits from Thread.
I implement the constructor, and destructor, and override the virtual void function, “run()”.

class myThread : public Thread{
public: myThread(const String&,size_t);
void run() override{}
~myThread(){}
};

After having declared the class, and not using it anywhere, it builds okay. However, when I try and create/initialise an object anywhere in my code, e.g.

myThread clockerThreader{ “name” , 0 }

the linker error occurs. If anyone could point me in the right direction I’d be very grateful

That’s not implemented.

Thats where the function is defined. Later then I initialise it and pass it parameters, it is initialised, no?

myThread(const String&, size_t);

is a declaration. You need a definition somewhere. For example

myThread::myThread(const String&, size_t) {}

or, in-class:

myThread(const String&, size_t) {}

You’re totally right, my stupid mistake. Thank you for pointing it out. I fixed it in my class:

class myThread : public Thread{
public: myThread(const String&,size_t){}
void run() override{
}
~myThread(){
}
};

However now it’s giving me the error:

“the constructor must explicitly initialise the base class juce::Thread which does not have a default constructor”

Because it doesn’t. You have to pass your parameters.

MyThread(const String& name, size_t stackSize) : Thread{ name, stackSize } {}

You should check your basics (or get some sleep, whichever applies).

1 Like

Ahh thank you so much man, it works! You’re right, I’m going to revisit some topics. Got too excited with Juce classes that I kinda skimmed over basic c++ things that I shouldn’t have.

Thanks again!

1 Like

@giannis-xri, for future reference, to make your code more readable, please indent every line with 4 spaces, or surround the whole code with three backticks before and after, like this:

```
void your_code_here ()
{
}
``` 

The same is obtained by selecting the code in your post while editing, then pressing the button that has this symbol on it: </>

Got it, thank you :slight_smile:

1 Like