# AsyncUpdater with a payload

**URL:** <https://forum.juce.com/t/asyncupdater-with-a-payload/35054>\
**Category:** Development\
**Created:** [September 3, 2019, 5:53am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054 "2019-09-03T05:53:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 5:53am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/1 "2019-09-03T05:53:00Z")

</div>

Hello JUCE devs,

Is it possible to extend **AsyncUpdater** functionality so that to let **triggerAsyncUpdate** to take an input parameter and send it as a payload to the **handleAsyncUpdate** method?  
This will let us put some logic into **handleAsyncUpdate** and decide what kind of updates the current component really needs.

Thanks,  
Arlen.

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 6:16am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/2 "2019-09-03T06:16:29Z")

</div>

Oh, by the way, currently I have the following implementation for your consideration. I’m not sure is it optimal or not. I’m sure you can come up with a better solution.

```auto
template<typename PayloadType>
class AsyncUpdaterWithPayload : public AsyncUpdater
{
public:
	auto triggerAsyncUpdate(PayloadType &&payload)
	{
		_payloads.add(std::forward<PayloadType>(payload));

		AsyncUpdater::triggerAsyncUpdate();
	}

	auto triggerAsyncUpdate(const PayloadType &payload)
	{
		_payloads.add(payload);

		AsyncUpdater::triggerAsyncUpdate();
	}

	auto getNextPayload()
	{
		return _payloads.removeAndReturn(0);
	}

	auto numberOfPendingPayloads() const
	{
		return _payloads.size();
	}

protected:
	Array<PayloadType> _payloads;
};

```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 3, 2019, 9:18am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/3 "2019-09-03T09:18:00Z")

</div>

Would this be a case for the [Message](https://docs.juce.com/develop/classMessage.html) and [MessageListener](https://docs.juce.com/develop/classMessageListener.html)?

Just curious…

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 3, 2019, 9:47am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/4 "2019-09-03T09:47:25Z")

</div>

From the docs for `AsyncUpdater::triggerAsyncUpdate`:

```auto
Causes the callback to be triggered at a later time.

This method returns immediately, after which a callback to the handleAsyncUpdate() method will be made by the message thread as soon as possible.

If an update callback is already pending but hasn't happened yet, calling this method will have no effect. 

```

This means that not every call to `triggerAsyncUpdate` will lead to a corresponding call to `handleAsyncUpdate`. So your idea is that you would have to loop over the pending payload elements in `handleAsyncUpdate`? If you would want to go that route, why don’t you implement this piece of work in your class too, like:

```auto
template<typename PayloadType>
class AsyncUpdaterWithPayload : private AsyncUpdater
{
public:
	void triggerAsyncUpdateWithPayload(PayloadType &&payload)
	{
		payloads.add(std::forward<PayloadType>(payload));

		triggerAsyncUpdate();
	}

	void triggerAsyncUpdateWithPayload(const PayloadType &payload)
	{
		payloads.add(payload);

		triggerAsyncUpdate();
	}

	virtual void handleAsyncUpdateWithPayload (const Payload& payload) = 0;

private:
    Array<PayloadType> payloads;
    void handleAsyncUpdate() override
    {
        for (auto& p : payloads)
            handleAsyncUpdateWithPayload (p);

        payloads.clear();
    }

};

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 3, 2019, 10:10am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/5 "2019-09-03T10:10:20Z")

</div>

The whole point of AsyncUpdater is that it coalesces events into a single callback, so it’s probably the least appropriate class to use if you need to know about each individual event!

Like Daniel said, that’s exactly what Message/MessageListener already does, or MessageManager::callAsync.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [September 3, 2019, 10:55am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/6 "2019-09-03T10:55:24Z")

</div>

> <https://github.com/jcredland/jcf_lime_juce/blob/master/utils/multi_async_updater.h>

We use this all the time. It’s pretty close to what you want.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [September 3, 2019, 12:50pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/7 "2019-09-03T12:50:09Z")

</div>

Worth pointing out that this isn’t threadsafe. The accesses to the `_payloads` array would need to be synchronised.

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 8:27pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/8 "2019-09-03T20:27:35Z")

</div>

> [@reuk](#):
>
> Worth pointing out that this isn’t threadsafe. The accesses to the `_payloads` array would need to be synchronised.

It IS thread safe. Look into the source code of juce\_Array:

```auto
    void add (const ElementType& newElement)
    {
        const ScopedLockType lock (getLock());
        values.add (newElement);
    }

```

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 8:30pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/9 "2019-09-03T20:30:08Z")

</div>

> [@daniel](#):
>
> Would this be a case for the [Message](https://docs.juce.com/develop/classMessage.html) and [MessageListener](https://docs.juce.com/develop/classMessageListener.html)?
> 
> Just curious…

Yeah. It really seems the case 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 8:32pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/10 "2019-09-03T20:32:20Z")

</div>

> [@jimc](#):
>
> We use this all the time. It’s pretty close to what you want.

Looks interesting. Thanks for the advice 🙂

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 8:34pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/11 "2019-09-03T20:34:17Z")

</div>

> [@jules](#):
>
> The whole point of AsyncUpdater is that it coalesces events into a single callback, so it’s probably the least appropriate class to use if you need to know about each individual event!

Yep. I see now. Thanks for the clarification 🙂

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 8:35pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/12 "2019-09-03T20:35:56Z")

</div>

> [@PluginPenguin](#):
>
> This means that not every call to `triggerAsyncUpdate` will lead to a corresponding call to `handleAsyncUpdate` .

Thanks. I see now 🙂

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 3, 2019, 9:00pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/13 "2019-09-03T21:00:25Z")

</div>

> [@Ptomaine](#):
>
> It IS thread safe. Look into the source code of juce\_Array:
> 
> ```auto
> void add (const ElementType& newElement)
> {
> const ScopedLockType lock (getLock());
> values.add (newElement);
> }
> 
> ```

Well if you look into the code then you should look at the whole code 😉  
The array has a second template parameter, the type of critical section to use. Per default this is a `DummyCriticalSection` object, which will lead to all those locks you see in the Array code to do nothing and be optimized away by the compiler. Only if you pass in a `CriticalSection` as second parameter, the Array becomes really thread safe. By the way the Array docs also say

```auto
To make all the array's methods thread-safe, pass in "CriticalSection" as the templated TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.

```

So you won’t even need to read the source code to get this information in this case. By the way, most of the juce classes are really well documented, so I would always study the docs first. Finally, this means for a thread safe array, you need to declare it like this: `Array<PayloadType, CriticalSection> _payloads`

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [September 3, 2019, 10:33pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/14 "2019-09-03T22:33:59Z")

</div>

Oops 🙂 You right. My bad.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [September 5, 2019, 8:30am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/15 "2019-09-05T08:30:46Z")

</div>

Also, if the payload you intend to send can be efficiently conveyed with a String, check out also ActionBroadcaster / ActionListener

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [September 5, 2019, 9:42am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/16 "2019-09-05T09:42:02Z")

</div>

Don’t strings allocate? that makes this not usable on the Audio Thread if that’s the case…

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 5, 2019, 11:03am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/17 "2019-09-05T11:03:17Z")

</div>

The Array used in the code snippet of the opening post allocates too, so if an allocation-free solution to be called the audio thread is needed, none of the solutions proposed in this thread should work…

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 5, 2019, 11:18am UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/18 "2019-09-05T11:18:04Z")

</div>

> [@matkatmusic](#):
>
> Don’t strings allocate? that makes this not usable on the Audio Thread if that’s the case…

That depends, what kind of string we are talking about, there are plenty…

- **juce::String** uses internally a ReferenceCountedObject to hold the data, so it allocates
- **std::string** is just a container of char, so it can be put on stack (not allocating) or heap (allocating) or moved…

The same rules like std::vector apply. I wonder though, what happens, if you assign a longer std::string to an existing std::string in stack memory… would it move on top of the stack, leaving the previous version empty?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [September 5, 2019, 12:40pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/19 "2019-09-05T12:40:25Z")

</div>

> [@daniel](#):
>
> **juce::String** uses internally a ReferenceCountedObject to hold the data, so it allocates

Constructing a fresh new String will allocate, but copy-constructing will just increment the refcount. This is different to the behaviour of `std::string`, which is not allowed to be implemented using refcounting.

> [@daniel](#):
>
> **std::string** is just a container of char, so it can be put on stack (not allocating) or heap (allocating) or moved…

I don’t think that’s necessarily true. Most string implementations will do the ‘small string optimisation’, so strings \< 24 chars (normally) will live on the stack rather than the heap, but implementations aren’t required to do that. In every implementation I’m aware of, longer strings will always live on the heap.

> [@daniel](#):
>
> I wonder though, what happens, if you assign a longer std::string to an existing std::string in stack memory…

If you assign a long string to a `std::string` that was previously holding a string short enough to enable SSO, then some heap memory will be allocated to hold the new long string.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 5, 2019, 12:44pm UTC](https://forum.juce.com/t/asyncupdater-with-a-payload/35054/20 "2019-09-05T12:44:18Z")

</div>

> [@reuk](#):
>
> In every implementation I’m aware of, longer strings will always live on the heap.

Thank you for correcting that. TBH, I got doubts after posting, but couldn’t find a good explanation.
