# \[SOLVED\] Particle Variable and Function registration when using THREADING

**URL:** <https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763>\
**Category:** Troubleshooting\
**Created:** [October 22, 2015, 1:01am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763 "2015-10-22T01:01:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [October 22, 2015, 1:01am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/1 "2015-10-22T01:01:46Z")

</div>

If another post I asked:

> Is anyone having problems with 0.4.7 not always registering variables/functions in the Particle Cloud? In other words, Particle CLI list does not show any Variables or Functions (and the device is online). ....

I now say:

The problem only occurred for me with SYSTEM\_THREAD(ENABLED), when DISABLED it registered okay in the cloud. The problem was not consistent across devices, ie it worked okay on some devices.

The same issue came up with firmware 0.4.6.

The fix was to move the registration calls early on in the setup() function, not down amongst it where I was waiting for Particle Cloud connection to come up before registration.

Case closed!

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [October 22, 2015, 2:45am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/2 "2015-10-22T02:45:27Z")

</div>

Glad to hear you got it working, although I’d still like to see your code to reproduce the issue. The functions and variables should appear no matter where they are in startup.

---

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [October 22, 2015, 2:55am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/3 "2015-10-22T02:55:04Z")

</div>

@mdma, the code (some 7000 lines of it) is pretty complicated (and no doubt the issue)!

Hopefully I will be able to set up some test code that reproduces the fault and get this to you if it does.

Note that I was waiting for WiFi connected and Particle Cloud connection before registration. Did this because prior to v0.4.6, I found that when there was no WiFi connection, the function registration blocked for some time. Have not checked if this block occurs with 0.4.6.

Back shortly.

---

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [October 22, 2015, 3:56am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/4 "2015-10-22T03:56:46Z")

</div>

@mdma, I said:

> I found that when there was no WiFi connection, the function registration blocked for some time.

FYI just measured how long it blocks for, around 100 secs. This is a bit long.

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [October 22, 2015, 4:09am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/5 "2015-10-22T04:09:25Z")

</div>

When calling system functions, like Particle.variable() the application thread still has to block waiting for the system.

In the meantime, a workaround is to use SEMI\_AUTOMATIC mode, and have:

```auto
void setup()
{
    Particle.variable(....);
    Particle.connect();
}

```

We can avoid this by taking additional steps to make some of the internal code truly thread safe so it’s run on the calling thread, rather than always on the system thread. This will happen in a future release. (Multithreading is still beta so we can work out these issues.)

---

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [October 22, 2015, 4:25am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/6 "2015-10-22T04:25:07Z")

</div>

@mdma, yep, have a define for enabling SEMI\_AUTOMATIC mode!

I had originally had it in this mode and then changed to AUTOMATIC with the upgrade to 0.4.6.

Now see, with thanks, that I should have left it the way it was.

Am getting on with the reproduction of the original issue (and of course, no issue so far…)

---

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [October 22, 2015, 5:12am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/7 "2015-10-22T05:12:05Z")

</div>

@mdma, of course, could not reproduce the fault with my scaffold…

I will report back if ever I come across it again and have more information.

Conclusion: not reproducable, not worth chasing at the moment.

Thanks @UMD

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [October 22, 2015, 5:12am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/8 "2015-10-22T05:12:29Z")

</div>

Thank you for investigating - much appreciated!

---

<div class="post-metadata">

**Author:** ![sazp96](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/sazp96/32/4153_2.png) [@sazp96](https://community.particle.io/u/sazp96)\
**Post date:** [June 12, 2016, 9:53pm UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/9 "2016-06-12T21:53:59Z")

</div>

Hey guys, I’m migrating an old app to use multithread and I’m experiencing this issues running 0.5.1.

Once I moved the Particle.variables and function declaration to the top of the Setup{} the problem goes away.

---

<div class="post-metadata">

**Author:** ![Jerware](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jerware/32/1516_2.png) [@Jerware](https://community.particle.io/u/Jerware)\
**Post date:** [May 15, 2017, 3:39am UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/10 "2017-05-15T03:39:25Z")

</div>

For what it’s worth, this issue still exists in 0.6.2. I have to move my function declarations to the top of Setup() to get them to register (semi\_automatic / system thread enabled). Glad this thread existed, but I wouldn’t quite call it [SOLVED] yet.

---

<div class="post-metadata">

**Author:** ![andrewa](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/andrewa/32/3926_2.png) [@andrewa](https://community.particle.io/u/andrewa)\
**Post date:** [December 1, 2017, 6:58pm UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/11 "2017-12-01T18:58:29Z")

</div>

I can confirm this issue still exists - my setup was like this:

```auto
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);

void setup() {
    Particle.connect();
}

void loop() {
    if (Particle.connected() && !hasRegisteredCloudFunction){
        bool result = Particle.function("asdf",asdf);
        hasRegisteredCloudFunction = true;
    }
}

```

The function did not register even though `result` was being returned as `true`. I had to move the function registration to the top of my setup function prior to the call to `Particle.connect()` for it to show up in the particle console.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [December 1, 2017, 7:36pm UTC](https://community.particle.io/t/solved-particle-variable-and-function-registration-when-using-threading/16763/12 "2017-12-01T19:36:45Z")

</div>

Here is the github issue for this:

> <https://github.com/particle-iot/firmware/issues/738>

Basically the message to the cloud that describes all the functions and variables is currently sent once and asynchronously with respect to user code during setup().

This requires both the firmware side and the cloud side to change in order to fix it and there is an easy work-around (declare early) so I don’t think this is currently scheduled.
