Global Variables vs. Queues

I have a component written in ESP IDF 5.3 that reads a SIM7080G modem. This modem has instantaneous and fixed information.

Instantaneous information would be, for example, the signal level, which changes all the time, or the GPS location.

And I have some fixed information, such as: IP and DNS and Installed Chip.

I would like to make this component well-made so that this data can be accessed by other applications/tasks.

The program is all written in C.

What is the best approach that you suggest?

A queue of size 1 with xQueueOverwrite or a global variable?

Remembering that I have information that is a char *, for example, such as the IP and DNS.

What suggestions do you have for having a program with good performance?

I’d make it dependent on which part drives the update.
Is the information polled by some other component or host application ?
Or do you want to set an update event form the modem task to an other component ?
If it’s the latter I’d use a queue with normal push / pop behavior of a reasonable depth. This would provide an event to trigger a task bundled with the data.
If the info is just polled from time to time I’d probably use a mutex or a critical section protected access to the public data set of the modem task.
I guess performance is not an issue here due to the rather slow (?) update rate.

This public data will only be searched sometimes, no other events need to be updated.

It is just one screen of information:

I also have no problem with concurrency.

Because this page will be accessed sometimes.

Only for configuration and debugging purposes.

But my question is:

Do I still keep this data in a queue or can I keep it in Global variables (Within the scope of this file?)

If you do not access this data from multiple threads (I assume this because you said you have no problem with concurrency), then keeping it in file global variables should be okay.

1 Like

I would really appreciate your reply. Thank you very much.

Means you can accept maybe inconsistent values.
When the modem task is preempted while updating the (global volatile) variables by an other task reading them, the data might be inconsistent.
Just for completeness reasons.

The solution to that issue is to somehow guard the update/retrieval of the data. Global variables could be guarded with a mutex or a critical section.

All that the 1 element queue solution does is make all accesses to the data be via a block copy in or out of the queue which will be done in a critical section. It is likely more efficient to do the accesses yourself in user code with similar protections.