FX:GBPCAD   Livre sterling / Dollar canadien
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;
Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.