The Photon is a quite advanced device compared to a bare AVR-based Arduino.

One interesting feature is that the platform is based on an embedded operating system; FreeRTOS. That means that we can code concurrent software (who said blinking multiple LEDs?). Of course, thread-like behavior is also possible on Arduino platforms using Protothreads, something similar to green threads. This article will only focus on OS-managed threading on the Photon.

Let's jump directly to an example:

Thread* worker;
void setup()
  worker = new Thread(NULL, longTask);
os_thread_return_t longTask(void* param) { delay(1000); }

As you can see, it is straightforward and similar to the Pthreads interface. But what to do when you want to share a resource between threads, like the i2c serial bus? Use a mutex!

Mutex i2c_lock = Mutex();

os_thread_return_t myLoop(void* param) {
  for(;;) {
    temperature = bme.readTemperature();

If your program encounter deadlock on the main loop, all the execution will hangs and your Photon will become useless. You really don't want this to happen on a Photon deployed in the field. Of course, if are correctly employing concurrency control it should not happen, but badly behaving concurrent flow can sometimes be very difficult to detect and debug. As a safety precaution, you can add a watchdog at the top of your sketch (before setup()).

// reset the system after 10 seconds if the application is unresponsive
ApplicationWatchdog wd(10000, System.reset);

The timer will be reset at every loop() iteration, so make sure that the timer value is bigger than the longest loop iteration you can expect.