Feb-21-2019, 12:48 PM
First, as a maxim, don't use globals. They're more trouble than they're worth.
Second, you need a thread safe data structure to conduct this operation. "Thread safe" means that the variable can only be accessed by one thread at a time which ensures consistency when any thread uses it. Without thread safety, there may be the possibility of thread1 evaluating the variable, thread 2 changes it, and then thread 1 runs a calculation with the changed variable and gets a nonsensical result. Read up on multithreading locks. Once a lock is implemented, the function will need the variable as an argument.
Second, you need a thread safe data structure to conduct this operation. "Thread safe" means that the variable can only be accessed by one thread at a time which ensures consistency when any thread uses it. Without thread safety, there may be the possibility of thread1 evaluating the variable, thread 2 changes it, and then thread 1 runs a calculation with the changed variable and gets a nonsensical result. Read up on multithreading locks. Once a lock is implemented, the function will need the variable as an argument.