dibosco wrote on Tuesday, November 10, 2015:
I’m trying to understand what is good practice in terms of accessing data created/modified in another thread.
It seems that a queue is obviously the safest way of passing data from one thread to another, but what about when one thread merely needs to know about the status of somehting in another thread?
Is it acceptable to have an accessor function that reads a static variable in the module the other thread is contained in, to read and return, say, a flag?
For example:
I have a thread that parses an XML file and puts together which of the sixteen DALI scenarios are valid.
I have another thread that receives UDP packets in, with one of the fields in the UDP packet a request to switch to a new DALI scenario. If the DALI scenario is not valid, according to the XML thread, the scenario should not be requested over the DALI bus.
In this case, I have created a static variable in the XML parsing module that is a bit map, each bit set or cleared to define whether a scenario is valid. Is it acceptable for the UDP thread just to request that bit map and find out whether a particular scenario is valid? Or must I send that bit map to the UDP thread via a queue? If so. must, or should, that bit map be sent to the UDP thread or is it acceptable just to have a static variable accessible by the whole of the module that contains the UDP thread?
I have in the past (in other projects) had a queue of just length of one 32-bit variable - which is 32 flags - and just written to and read/peeked from/at that queue from lots of threads so all threads can at any time just look at the flags or read them out, modify them and restore them. In the end this is more of a faff. However, it may be that it is the only really thread-safe way of doing things?
Or is there a third way I have not thought of?
Having a separate queue for all different types of data for different threads to access like this soon really eats into precious RAM.
Many thanks