Is the class IndicatorDataSeries thread safe?

Created at 10 Feb 2020, 01:29
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KO

koktos632

Joined 18.01.2020

Is the class IndicatorDataSeries thread safe?
10 Feb 2020, 01:29


Hello!

I have to perform some calculations in the background thread.

Is the class IndicatorDataSeries thread safe?
Is it safe to update IndicatorDataSeries objects from outside of Calculate(...) `s thread?
 

Regards,


@koktos632
Replies

firemyst
10 Feb 2020, 04:45

RE:

koktos632 said:

Hello!

I have to perform some calculations in the background thread.

Is the class IndicatorDataSeries thread safe?
Is it safe to update IndicatorDataSeries objects from outside of Calculate(...) `s thread?
 

Regards,

I do all the time as long as the index you're modifying isn't the current index as per the parameter in the Calculate method. This is because unless you implement locking, the calculate method could try and update the index the same time you are.

Here's a quick code example of what you might put in both the Calculate and your other methods to control the locking.

Create your own "_lockObject" at the global class level.

The below code will try to enter and perform the actions within the try/finally block. It will wait for the amount of milliseconds specified to acquire the lock. If it can't, it will skip the code; otherwise the code will execute.

//Check for the lock once out here instead of in each individual method.
                    if (Monitor.TryEnter(_lockObject, CONST_maxWaitToAcquireLockInMilliseconds))
                    {
                        try
                        {
                            //do whatever you have to here
                        }
                        finally
                        {
                            // Ensure that the lock is released.
                            Monitor.Exit(_lockObject);
                        }
                    }
                    else
                    {
                        Print("Could not acquire lock. Skipping.");
                    }

 


@firemyst

koktos632
10 Feb 2020, 08:44

RE: RE:

firemyst said:

Here's a quick code example of what you might put in both the Calculate and your other methods to control the locking.

Thank you for the example.
The questions is still not answered.

2 support:
Is the class IndicatorDataSeries thread safe?
Could you answer, please?

 


@koktos632

PanagiotisCharalampous
10 Feb 2020, 09:03

Hi koktos632,

No it is not.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

koktos632
10 Feb 2020, 09:43

RE:

PanagiotisCharalampous said:

No it is not.

Add it to docs, please.
Thank you!


@koktos632