Global Variables Explained?



  • @Fox @support - I could not find anything related to the following question(s). I would like to know the logic behind the threading, and if there are any Do's & Dont's behind it.
    How thread safe/reliable are Global Variables between threads?
    Does the P() & PSet() invoke and have the other threads 'Wait' until it's finished? (I believe so)
    If yes, the chances of mixed data is very slim - but still able to mix?

    In theory if the data can be mixed, it's because it's pulling the global data -> +1 to o.temp1 -> Write back to Global. The writing could be 'old' data from thread_id2 getting the global data before thread_id1 could write the new one.
    Thread_ID1 -> PULLS
    Thread_ID2 -> PULLS
    Thread_ID1-> Writes (temp1 = 1)
    Thread_ID2 ->Writes (temp1 = 1) [Where it should be temp1 = 2]

    Take the following code for instance; the End Results are correct;
    {"temp1":39,"temp2":1,"temp3":{"tmpA":"","tmpB":[]}} - 10 Threads, 30 Success - sleep(200)!
    {"temp1":649,"temp2":1,"temp3":{"tmpA":"","tmpB":[]}} - 300 Threads, 350 Success - sleep(200)!
    {"temp1":15299,"temp2":1,"temp3":{"tmpA":"","tmpB":[]}} - 300 Threads, 15000 Success - sleep(200)!
    {"temp1":15299,"temp2":1,"temp3":{"tmpA":"","tmpB":[]}} - 300 Threads, 15000 Success - sleep(rand(100,3000))!

    function TheThreads() {
      var o = JSON.parse(P("basglobal", "GlobalVar") || '""');
      o.temp1 = (o.temp1 + 1);
      PSet("basglobal", "GlobalVar", JSON.stringify(o));
      sleep(200)!
    }
    // Remeber to do function in first 'Execute code' and another Execute code for the 'Main'
    PSet("basglobal", "GlobalVar", JSON.stringify({
      temp1: 0,
      temp2: 1,
      temp3: {
        tmpA: "",
        tmpB: []
      }
    }));
    
    log(P("basglobal", "GlobalVar") || '""');
    
    _call_section(TheThreads, 10, 30, 30)!
    
    log(P("basglobal", "GlobalVar") || '""');
    
    

    Even adding multiple sections seems to come out Correct! Seems like there are no 'Dont's' ?

    function PoolFirst() {
      var o = JSON.parse(P("basglobal", "Pools") || '""');
      _if_else(o.SecondPool == true, function () {
        o.SecondPool = false
        PSet("basglobal", "Pools", JSON.stringify(o));
        _call_section(PoolSecond, 11, 500, 30)!
      }, function() {
        var o = JSON.parse(P("basglobal", "GlobalVar") || '""');
        o.temp1 = (o.temp1 + 1);
        PSet("basglobal", "GlobalVar", JSON.stringify(o));
        sleep(rand(100, 2000))!
      })!
    }
    
    function PoolSecond() {
      var o = JSON.parse(P("basglobal", "Pools") || '""');
      _if_else(o.ThirdPool == true, function () {
        o.ThirdPool = false
        PSet("basglobal", "Pools", JSON.stringify(o));
        _call_section(PoolThird, 1, 50, 30)!
      }, function() {
        var o = JSON.parse(P("basglobal", "GlobalVar") || '""');
        o.temp2 = (o.temp2 + 1);
        PSet("basglobal", "GlobalVar", JSON.stringify(o));
        sleep(rand(100, 2000))!
      })!
    }
    
    function PoolThird() {
      var o = JSON.parse(P("basglobal", "GlobalVar") || '""');
      o.temp3.tmpB.push(o.temp1)
      PSet("basglobal", "GlobalVar", JSON.stringify(o));
      sleep(rand(100,2000))!
    }
    
    PSet("basglobal", "Pools", JSON.stringify({
      SecondPool: true,
      ThirdPool: true
    }));
    
    PSet("basglobal", "GlobalVar", JSON.stringify({
      temp1: 0,
      temp2: 1,
      temp3: {
        tmpA: "",
        tmpB: []
      }
    }));
    
    log(P("basglobal", "GlobalVar") || '""');
    
    _call_section(PoolFirst, 101, 5000, 30)!
    log((JSON.parse(P("basglobal", "GlobalVar"))).temp3.tmpB.length)
    log(P("basglobal", "GlobalVar"));
    

    Thanks you!



  • @support @Fox Still wondering about these Do's & Don'ts and insight how it works with Global Variables in Threading. :)



  • @allister said in Global Variables Explained?:

    @support @Fox Still wondering about these Do's & Don'ts and insight how it works with Global Variables in Threading. :)

    Any sequence of synchronous actions is performed in one thread. This means that execution cannot be transferred to another thread, so it cannot violate the integrity of the data.
    This applies only to synchronous actions, during the execution of asynchronous, execution can be transferred to another thread.
    https://www.youtube.com/watch?v=Vj2Q50FIBi4&t=4770s - here is very detailed with examples.



  • @fox said in Global Variables Explained?:

    https://www.youtube.com/watch?v=Vj2Q50FIBi4&t=4770s - here is very detailed with examples.

    I've came across this video but the translation was not very accurate, I will try again to get it to translate correctly. Thank you!


Log in to reply