We are working on an embedded system on which several tasks have to be performed simultaneously. We were abel to create threads that run in parallel.
Everyone tells us that global variables are bad. But how can we share data between two threads without using them? Is there an easy freamework with an example how to share data between threads?
We would also like to share an “event” between the main program and a thread to get feedback when a thread has finished initialisation.
Please provide some more details what you have to communicate between threads and how the setup looks like, so may someone could help you further then.
Thanks for your answer. I’ve looked at the Interrupt Lib Demo and there the Events are defined globally as well!
My question is, can this be done better or is this the way to go? We will have many threads, so there have to be many globally defined Event-Handles?
Also: We would like to share several variables (e.g. a float-array) so Thread_1 can write data in this array and it can be read by Thread_2. I found winCE-Mailboxes that do something like this … but it looks very complicated. Windows CE .NET Interprocess Communication | Microsoft Learn
Sometimes global variables are not so bad, in most cases the worst is how we use them. The synchronization between threads with events is the way to go, but this does not mean you have to use a global variable, you could encapsulate the event and generate a public function such as “waitForCompletion ()”.
In the case of sharing data mutex and volatile are your friends, the subject is extensive but you could start reading this:
Finally, for a more professional production code I recommend you the Intel TBB(Widely used C++ template library for task parallelism), check this link: