MQL4 - automated forex trading   /  

Documentation

MQL4 Reference  Global variables  GlobalVariableSetOnCondition

 
Use the opportunities of
MQL5 in MetaTrader 5
Study the new MQL5 language
Study the new MQL5 language.
Reference is already available
bool GlobalVariableSetOnCondition( string name, double value, double check_value)
Sets the new value of the existing global variable if the current value equals to the third parameter check_value. If there is no global variable, the function will generate error ERR_GLOBAL_VARIABLE_NOT_FOUND (4058) and return FALSE. When successfully executed, the function returns TRUE, otherwise, it returns FALSE. To get the detailed error information, one has to call the GetLastError() function.
If the current value of the global variable differs from the check_value, the function will return FALSE.
The function provides atomic access to the global variable, this is why it can be used for providing of a semaphore at interaction of several experts working simultaneously within one client terminal.
Parameters:
name   -   Global variable name.
value   -   New value.
check_value   -   Value to be compared to the current global variable value.
Sample:
  int init()
    {
     //---- create global variable
     GlobalVariableSet("DATAFILE_SEM",0);
     //...
    }
  
  int start()
    {
     //---- try to lock common resource
     while(!IsStopped())
       {
        //---- locking
        if(GlobalVariableSetOnCondition("DATAFILE_SEM",1,0)==true)  break;
        //---- may the variable be deleted?
        if(GetLastError()==ERR_GLOBAL_VARIABLE_NOT_FOUND) return(0);
        //---- sleeping
        Sleep(500);
       }
     //---- resource locked
     // ... do some work
     //---- unlock resource
     GlobalVariableSet("DATAFILE_SEM",0);
    }