Page 1 of 1
Mutithread variable share
Posted: Wed May 18, 2022 12:46 am
by leon_titan
Code: Select all
bool start = false;
// thread 1
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {
while(true) {
uprintf(": %u\n", start);
}
}
// main thread
void somefunc() {
start = true;
}
How to share variables between threads? I use the code above but the value of "start" is changed not as expected. When I change the start to true, the print is like:
Why and how to do that correct?
Re: Mutithread variable share
Posted: Wed May 18, 2022 5:30 am
by Giovanni
Hi,
Make the variable "volatile" or the compiled will optimize it out.
Giovanni
Re: Mutithread variable share
Posted: Wed May 18, 2022 9:02 am
by leon_titan
Got it. Thx for reply.
Re: Mutithread variable share
Posted: Wed May 18, 2022 3:04 pm
by leon_titan
after add volatile still got the same result

Re: Mutithread variable share
Posted: Wed May 18, 2022 4:10 pm
by Giovanni
I don't see enough code for an explanation. It is possible that thread is starving the others, there are no sleep functions called inside the loop.
Giovanni