FX:GBPCAD   British Pound / Canadian Dollar
if( _cursec != "" )
{
StaticVarSet( _cursec, 0 );
_cursec = "";
}
}
function TimeConsumingWork()
{
// WARNING: the Percentile is CPU hungry as it involves lots of sorting,
the loop below may take > 1 second to complete
for( i = 0; i< 10; i++ ) Percentile( C, 100, 10 );
}
//_TRACE("Without CS Begin " + GetChartID() );
//TimeConsumingWork(); // some time consuming calculation
//_TRACE("Without CS End" + GetChartID() );
// Example usage (critical section)
if( _TryEnterCS( "mysemaphore" ) )
{
// you are inside critical section now
_TRACE("Begin CS " + GetChartID() );
TimeConsumingWork(); // some time consuming calculation
_TRACE("End CS " + GetChartID() );
_LeaveCS();
}
else
{
_TRACE("Unable to enter CS");
// EXAMPLE 1 : Simple semaphore (no waiting)
if( StaticVarCompareExchange( "semaphore", 1, 0 ) == 0 ) // obtain
semaphore
{
// protected section here
// Here you have exclusive access (no other threads that check for
semaphore will enter simultaneously)
/////////////////////////
StaticVarSet("semaphore", 0 ); // reset semaphore
}
else
{
_TRACE("Can not obtain semaphore");
}
///////////////
// EXAMPLE 2 HOW TO IMPLEMENT CRITICAL SECTION IN AFL
///////////////
function _TryEnterCS( secname )
{
global _cursec;
_cursec= "";
// try obtaining semaphore for 1000 ms
for( i = 0; i < 1000; i++ )
if( StaticVarCompareExchange( secname, 1, 0 ) == 0 )
{
_cursec = secname;
break;
}
else ThreadSleep( 1 ); //sleep one millisecond
return _cursec != "";
}
// call it ONLY when _TryEnterCS returned TRUE !
function _LeaveCS()
{
global _cursec;
Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.