Threading
Posted: Mon Aug 03, 2020 4:09 am
Hello,
I'm trying to control multiple stepper motors/limit switches with a single raspberry pi. I am writing a program to do this. My thought was to have multiple threads which call the same function and each motor would move based on the functions called and corresponding limit switch position (NC/NO etc..).
My concern is:
If two separate threads were created which called the same function, that had a variable; would each thread iteration change the variable value for each other or would the variable changes be contained to each thread?
For example:
*Threads "A" and "B" created/called*
If thread A called a function "C()" and changes its variable "c" to 1 and thread B also called function "C()" and also changes variable "c", but this time to 2. Would thread A, on its next iteration, now read a "c" value of 2?
(I was trying to test this myself but ending up running into some results that were a bit perplexing. So just hoping to get a second opinion.)
Thanks in advance for the help!
I'm trying to control multiple stepper motors/limit switches with a single raspberry pi. I am writing a program to do this. My thought was to have multiple threads which call the same function and each motor would move based on the functions called and corresponding limit switch position (NC/NO etc..).
My concern is:
If two separate threads were created which called the same function, that had a variable; would each thread iteration change the variable value for each other or would the variable changes be contained to each thread?
For example:
*Threads "A" and "B" created/called*
If thread A called a function "C()" and changes its variable "c" to 1 and thread B also called function "C()" and also changes variable "c", but this time to 2. Would thread A, on its next iteration, now read a "c" value of 2?
(I was trying to test this myself but ending up running into some results that were a bit perplexing. So just hoping to get a second opinion.)
Thanks in advance for the help!