Threads and Mutex for reading sensors

Hi everyone! I am asking you help and suggestions about my problem. I never used so far threads and mutex. In my case I have an M3 board on Iot-LAb at inria , and I have developed a program source which read sensors values in an infinite loop. There are 4 sensors on the board and my program can read continuosly the sensors values, or can read by giving a start/stop command. I have used the threads but I am not sure of that. My first question is : am I forced to use threads for readings multiple sensors on the board? I mean can happen some conflict in this multiple readings ? The other question is, once we suppose of using a thread loop for each single sensor reading (so I will have a 4 threads, one for each sensor) , should I use a single mutex lock variable, or should I use 4 different mutex lock variables? I attach here enclosed my program source so that you can give me some hints about it , since it does not work yet. Bye and thank you for your help.

No. You can also just have one thread, even use the main thread for that. You might want to use multiple though in certain scenarios.

There isn’t even a need for a single mutex. When you open a thread for each sensor, one thread has exclusive access to the sensor. The HAL of RIOT is managing the access to the bus. Take a look for example in the aquire implementation of I2C.

Thank you. This is also my conclusion. Though I found a template using many thread and mutex also. I do not know why.

Do you have a link to the template? Is it in the mainline riot repo?

Yes. The template is in the sensors folder in jupiter notebook on

The template is probably not maintained. I would rather take a look into the RIOT repos.

I’m the author of this notebook and it should not be considered as a template, but as an exercise. The goal of this exercise is to put together several RIOT concepts (thread synchronization, shell, different sensor drivers) presented before in separate learning materials, like the RIOT course (more precisely this section which ends with this notebook). These notebooks are also used in a Mooc on IoT (next session is coming before the end of the year).

It is ! and this notebook is referenced from the RIOT course (so under the RIOT-OS orga). The source code of all the notebooks is available in this IoT-LAB GitHub repo and the IoT-LAB team updates them regularly. There’s also a JupyterHub server running at so you can try them with just an IoT-LAB account and a browser.

1 Like