Global Variable is not increasing on each new thread



  • I'm having a weird issue all of a sudden:

    I set global variable before creating threads. Let's say value of 5 (int).

    I create 10 threads, and wish to have each thread increase it by 1, so I "Increase global variable" at the start of the thread function, but each thread uses the 6 value, not 6,7,8,9,10, etc...

    Before "Call function in several threads":
    9b3ebc7c-2e6d-4f36-a622-d7aca8dead44-image.png

    Inside the fucntion:
    56e3a53c-4f4f-4d66-b246-6248baa169ce-image.png

    Output when ran:
    alt text



  • Why you use "Call function in several threads"?

    If you use it for increasing global or local variable
    it will probably increase just once, since its just ONE
    function call - so it works as expected

    Use "Thread number" instead and set it to number you need.
    Than increasing of number should work fine.



  • @GaG said in Global Variable is not increasing on each new thread:

    Why you use "Call function in several threads"?

    If you use it for increasing global or local variable
    it will probably increase just once, since its just ONE
    function call - so it works as expected

    Use "Thread number" instead and set it to number you need.
    Than increasing of number should work fine.

    Thanks for your reply. I attempted to increase thread number at the top, and just calling the function in each thread. However, no luck.

    So I guess, for some reason, my brain is blocking logic; are you referring to:

    3509f31d-ffdb-4e87-8281-85ca2bd63d39-image.png



  • Yes, but dont use "Call function in several threads". Just
    increase number inside your script. So your script would
    execute 10 times and in each execution it will increase global var by 1.



  • This method did not work for me. Do you see something I'm doing wrong?

    (with a increase global variable inside that function)

    Still outputs all same number.

    8f3066e7-fc09-411b-8d7e-8f1efd98f54b-image.png



  • Set global variable as function with option "OnAplicationStart"
    That way it will set starting number only once.

    In your current code it resets to starting value on every thread execution.
    Maybe it was the case even with "Call function in several threads", test it.



  • I got it, with some other logic.

    I had to redefine global variable while inside the thread, then increase it.


Log in to reply