Using one method simultaneously by two threads


#1

Hello,
I have question more in C++ than in JUCE, but not sure how to find the answer.

I know that I can’t use by two threads at the same time one variable. But what about method of that variable?

I am programming audio compressor. And in AudioProcessor I have method calculateAttenuator(float envelope, float &treshold, float &ratio, float &knee).

And I use that method in AudioProcessor.processBlock(). But I also want to draw the curve of that compressor. So in other component I would like to use something like that:

for(float i=0.0f; i<100; ++i)
{
      myPath.lineTo(i, processor.calculateAttenuator(i, treshold, ratio, knee);
}

repaint();

I am asking for curious, for the future. At the moment I created separated class Compressor with method calculateAttenuator, and made separated instances of that Compressor for AudioProcessor, and for myDrawingComponent. But although that I wonder how sharing methods between threads works?

As I know we can use lambdas which are pointers to methods. I think in that way:
if lambdas can pointing methods, so methods occupy some part of memory. So it seems to be not good to use the same memory at the same time by two threads. Am I right? Or for methods it doesn’t make a sense?

Thanks for any help.


#2

It’s all about the variables that are accessed, there’s not much else to consider. Local stack allocated variables in functions are thread safe because each thread has its own stack. If your calculateAttenuator method uses member variables of the class or pointers or references to some external state, it’s very likely not thread safe. So multiple threads likely should not access the same object instance without synchronization.


#3

Yes I forgot to mentioned, that all variables in method are temporary (created in method). So it’s not problem? Great.


#4

Why is the function a class method, then?


#5

Why is the function a class method, then?

Actually I don’t know :slight_smile:
Probably because I don’t have good habits in programming, but I thought it is just clear to understand, that calculating attenuator is in Compressor class, where I have other things which are concern to Compressor, like calculating envelope, or attack time, and things like that. So I wanted to keep all those things in one class. Isn’t it rational?


#6

I guess, but at least you could make it a static method, so you don’t accidentally make it refer any object specific member state in the future.


#7

OK I will consider it but I am still not very familiar with static at all. I know what it does and how does it work, but don’t feel it ( I know but I am not sure of it - something like that - especially in the context of methods, I have some mess in mind with all those const, static, mutable) Definitely I need to learn those things. Thanks for your advice.


#8

static has lots of meanings in C++ but static methods are really quite simple. It’s just a function in a class that can’t access object instance specific data from the class.