Whats best way to communicate between threads in WinCE with C++?

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.

Beni

@Beni3141: Which programming language are you using under which OS? (C++, C, C#, CE version).

Its C++ with VisualStudio 2013, I’ve added it now to the question.

It is hard to say what is the best to do the job. It depends on how you want to use the threads and what kind of data you need to share if any.

Some mechanisms that are valuable when working with threads:

  • Events: Signalize or trigger actions to one or the other thread. See for example the Interrupt Lib Demo of our library package.
  • Critical Sections, Semaphores and Mutexes: Used to synchronize access on shared resources (See also https://msdn.microsoft.com/en-us/library/ee488929.aspx) .

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. https://msdn.microsoft.com/en-us/library/ms836784.aspx

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:
http://www.drdobbs.com/cpp/volatile-the-multithreaded-programmers-b/184403766

Finally, for a more professional production code I recommend you the Intel TBB (Widely used C++ template library for task parallelism), check this link:

https://www.threadingbuildingblocks.org/files/documentation/a00129.html