Particle threads tutorial


#21

Thanks but what headers do I need to include in my application to access the underlying method? Just include "FreeRTOS.h doesn’t work.


#22

How do I ensure that the Threads begin only after the setup() has completed?

Can the below statement be written inside the setup() instead of at the top of the file?

Also, the below logic doesn’t seem to work for me.

void threadFunction(void)
{
     /* Wait for the setup() to complete */
    while( !gSetupCompleted )
    {
        continue;
    }
    while(true)
    {
        // actual thread doing the task
    }
}

where gSetupCompleted is set to true at the end inside setup().

Thanks | Regards,
Dipen


#23

There already is a post in this thread that shows how you can

But also in the opening post Rick has shown how to use a mutex to hold your thread off from running.


#24

Namaste @ScruffR,

Thank you for your prompt reply. I should have gone through the entire post before posting my query :wink:

The post from @justicefreed_amper was just what I was looking for. So thanks to @justicefreed_amper too!

I’ve implemented the logic to start the Threads just before exiting the setup() and, my firmware is working as desired.

Thanks | Regards,
Dipen


#25

Just make sure to keep in mind that my first post in that thread only works in firmware versions v0.6.4 and below - I’m now personally in favor of using a boolean flag to start and pause the thread:

bool run_thread = false;
...
void thread_function(void) {
     while (run_thread == false) delay(100);
     // do init / setup for your thread here
     while (true) {
          while(run_thread == false) delay(100);
          // do main thread stuff here
          delay(40);
     }
}

setup() {
     // setup stuff
     ...
     SINGLE_THREADED_BLOCK {
          run_thread = true;
     }
}

#26

@justicefreed_amper, @ScruffR, if the above holds valid, I’m surprised that my firmware is not creating problems with Device OS ver 1.1.0. My implementation is exactly as suggested in the post #4 of this thread. It’s over 24 hours now since the firmware is running and readings are successfully getting published on the Cloud. No issues have been observed, at least till now.

Thanks | Regards,
Dipen


#27

This comment in post #5 may expain that

However, I’d rather use Thread *myThread; and myThread = new Thread("myThreadName", myThreadFunction); to avoid creation of a dummy object.


#28

@ScruffR @rickkas7

When using mutexes, I ended up having to put

myThread = new Thread("myThreadName", myThreadFunction);

in the setup() function so that the mutex init (i.e. os_mutex_create(&mutex);) could be guaranteed to happen before the thread executed.

If I placed the os_mutex_create() call in the STARTUP() block per the example, it failed with an SOS:1 message when the os_mutex_lock() was called in the thread.

On system firmware 1.0.1.


#29

Same issue in DeviceSO 1.4.1-rc.1