Do i need mutex or queue in my situation

My situation is that I need to send the temperature data under every 20- 30 seconds to a remote server.

I have 2 threads one thread read temperature data and another thread send temperature data to server with the help of tcp socket.

I am planning to use global variable that can be accessible in both threads.

My question

Do I need to use mutex or queue in my situation to share variable?

If No, than can you please explain why i don’t need?

ESP32 hardware.

This question i am asking just to understand concept.

Basically If the temperature is stored in an atomic datatype on your platform like a 32 bit integer you can change this (volatile !) variable and read it without access protection like a mutex because the reader always reads a consistent value.
For instance if you have a structure of data with multiple elements and you want to write to and read from it as a consistent set of data, you have to mutually exclude (using a mutex or by an enclosing criticial section) the access to it because you can’t atomically change all elements at once and a reader could read a mix of outdated and updated elements.
In your specific case why not just read the temperature and send it to the server in 1 task periodically ?


I understood that both the tasks should be kept on equal priority. But I do not understand how to schedule both the tasks.

Thread 1 runs and blocks for 5 seconds ( Read Temperatures)
Thread 2 runs and blocks for 10 seconds ( send data to server)

should i schedule tasks in this way ?

The prio doesn’t really matter since both tasks block for while giving up the CPU allowing other tasks to run.
If you only want to send the temperature every 10s to the server just read and send it every 10 s.

It should be schedule like this

Thread 1 runs and blocks for 10 seconds ( Read Temperatures)
Thread 2 runs and blocks for 10 seconds ( send data to server)

If you use a queue, you don’t need a global variable. Just send the temperature reading to the queue and read them from the other task when needed.
Although if both tasks run with the same periodicity, perhaps you could do both the temperature reading and the tcp send in the same task?