Particle.publish() with multithreaded applications

I’m struggling to find any existing questions which answer this question. I’m an experienced developer, but a noob with Particle (just purchased an Electron).

I already have some variables and functions which are working. Separately, I have multiple threads working. However, if I have a few strange problems next:

  • If I try to move Particle.process() from loop() into one of my threads, it never connects to cloud.
  • If I call Particle.publish() from on of my threads, it loses connection with the cloud.
  • I have also confirmed that os_thread_delay_until() is working as expected in my two threads.
  • Additionally, I I tried wrapping process (from loop()) as well as publish (from the other task) with SINGLE_THREADED_BLOCK(), but that did not seem to help.
  • I have also tried understanding the various settings for SYSTEM_THREAD and SYSTEM_MODE, but that did not seem to answer anything for me either.

I’m guessing there’s something I’m fundamentally not understanding about what happens when process() or publish() are called, so I’m hoping someone with more experience with Particle can point me in the right direction.

With SYSTEM_THREAD(ENABLED) you should not need to call Particle.process() from user code - and best even avoid trying :wink:

Since without SYSTEM_THREAD(ENABLED) Particle.process() is called between iterations of loop() calling it from another thread too may cause problems.

For async Particle.publish() calls you can have a look at this library

Thank you! This did the trick!

I have to admit I did come across the library, but I previously believed there was something else I was doing wrong, and I didn’t want to blindly start pulling in libraries without first understanding.