Login Page - Create Account

Support Board


Date/Time: Wed, 27 Nov 2024 16:47:01 +0000



Post From: C++ threads inside a ACSIL study?

[2023-02-08 23:15:19]
User133994 - Posts: 80
Verified...apparently *any* std::thread crashes SC even before you get to add it to a chart. Hmmm... Hopefully, someone has a work around.

Support, Please verify my findings and share any work arounds. Thanks so much.

This simple ACSIL study CRASHES Sierra Chart as mentioned above:

(to stop the Crashes you must delete the threadTest.dll file from the "Data" folder)


#include "sierrachart.h"
#include<thread>

/****************************************************************************/

SCDLLName("threadTest")

// A dummy function
void foo(int Z)
{
SCString DebugString;
for (int i = 0; i < Z; i++)
{
DebugString.Format("example thread loop");
// sc.AddMessageToLog(DebugString, 0);
// cout << "Thread using function"
   // " pointer as callable\n";
}
}

/*============================================================================
  This example code calculates a simple moving average (30 period by
  default).
----------------------------------------------------------------------------*/
SCSFExport scsf_SimpMovAvg(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_Average = sc.Subgraph[0];
  
  SCInputRef Input_Length = sc.Input[0];
  
std::thread th1(foo, 3);
  // Set configuration variables
  if (sc.SetDefaults)
  {
    // Set the configuration and defaults
    
    sc.GraphName = "Simple Moving Average";
    
    sc.StudyDescription = "Example function for calculating a simple moving average.";
    
    // Set the region to draw the graph in. Region zero is the main
    // price graph region.
    sc.GraphRegion = 0;

    sc.ValueFormat = VALUEFORMAT_INHERITED;
    
    // Set the name of the first subgraph
    Subgraph_Average.Name = "Average";
    
    // Set the color, style and line width for the subgraph
    Subgraph_Average.PrimaryColor = RGB(0,255,0);
    Subgraph_Average.DrawStyle = DRAWSTYLE_LINE;
    Subgraph_Average.LineWidth = 2;
    
    // Set the Length input and default it to 30
    Input_Length.Name = "Length";
    Input_Length.SetInt(30);
    Input_Length.SetIntLimits(1,MAX_STUDY_LENGTH);
    Input_Length.SetDescription("The number of bars to average.");
    
    sc.AutoLoop = 1;
    
    sc.AlertOnlyOncePerBar = true;
    
    // Must return before doing any data processing if sc.SetDefaults is set
    return;
  }
  
  // Do data processing
  
  // Set the index of the first array element to begin drawing at
  sc.DataStartIndex = Input_Length.GetInt() - 1;
  
  
  // Calculate a simple moving average from the base data
  sc.SimpleMovAvg(sc.Close,Subgraph_Average,Input_Length.GetInt());
  
  
  if (sc.CrossOver(Subgraph_Average, sc.Close))
  {
    // Since we are using auto-looping we do not specify the Index parameter.
    sc.SetAlert(1, "Moving average has crossed last price.");
  }
}