EV3 BCC / Sharing variables between threads
Posted: 25 Oct 2013, 20:40
As this thread turned into some talk about multi-threading using C/C++, I think
I should make people aware of another problem: sharing variables between threads.
A double is 64bits long on ARM, so the cpu needs (at least) two operations to read or write it.
What now might happen if both threads are running is the following:
That's why I've used std::atomic<int> instead of just int in my previous C++11 thread example.
And as a warning: the C standard doesn't guarantee that even operations on native types (like integers) are done as an atomic operation (in one part). You might have luck with such (it depends on the compiler-(version) and some other things), but to be sure you have to use either atomic operations or you need to use mutexes, semaphores or similiar to protect reads and writes on shared variables from simultaneous access. (And don't use plain integers as replacement for a real mutexes.)
Don't forget, the EV3 uses a real operating system and real threads are a lot different than e.g. threads in python, nbc or nxc.
Alexander Holler
I should make people aware of another problem: sharing variables between threads.
Code: Select all
double d;
void thread_1(void)
{
...
d+=10;
}
void thread_2(void)
{
double my_d = d;
...
}
What now might happen if both threads are running is the following:
Code: Select all
thread_1 thread_2
---------------------------------------------
writes lower part of d -
- reads lower part of d
- reads upper part of d
writes upper part of d -
And as a warning: the C standard doesn't guarantee that even operations on native types (like integers) are done as an atomic operation (in one part). You might have luck with such (it depends on the compiler-(version) and some other things), but to be sure you have to use either atomic operations or you need to use mutexes, semaphores or similiar to protect reads and writes on shared variables from simultaneous access. (And don't use plain integers as replacement for a real mutexes.)
Don't forget, the EV3 uses a real operating system and real threads are a lot different than e.g. threads in python, nbc or nxc.
Alexander Holler