# Stream buffers vs message queues

**URL:** <https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739>\
**Category:** Kernel\
**Created:** [June 1, 2021, 12:26am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739 "2021-06-01T00:26:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 12:26am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/1 "2021-06-01T00:26:37Z")

</div>

Clarifying the concepts regarding message buffers and queues.

According to [this response](https://forums.freertos.org/t/stream-buffers-vs-message-buffers-vs-queues/8367/4), queues are used when there are multiple readers/writers and when you want to send a fixed-sized buffer, whereas, with stream buffers, there has to be a single reader/writer unless you use mutexes and the size of the data passed doesn’t always have to be the same.

Are these points really what set each other apart? What really makes a queue not accommodate variable-sized data? Looks like queues use linked list as an underlying data structure and stream buffers dynamic arrays. Is there a high level visual for each data structure that helps understand better?

I have a “main task” that basically will have some sort of a data structure used to receive stuff from multiple other tasks, and the main task parses it, and executes functionality accordingly.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 1, 2021, 12:50am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/2 "2021-06-01T00:50:24Z")

</div>

A Queue will always copy in and out a fixed sized block of data. To handle variable sized chunks, either you need to make multiple calls to send the full buffer (and a mutex to make sure no one else inserts something into the middle of the sequence) or you send a pointer (which is a fixed size).

StreamBuffers can sort of be thought as a Queue of bytes that add the ability to send/receive multiple bytes at once with a count, and the stream buffer will allow for a partial transfer. The single sender/single receiver limitation is added to allow it to be much more light weight, you can add a Mutex to get around this, or just use a regular Queue.

MessageBuffers use most of the same code as StreamBuffers, but automatically add a ‘MessageLength’ to the message, and disables the ability to send a partial message.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 1:01am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/3 "2021-06-01T01:01:48Z")

</div>

> you send a pointer (which is a fixed size).

…which is possible by setting the `uxItemSize` parameter of `xQueueCreate` to whatever the size of the pointer on the said platform is (on cortex M4, it’s 32 bits so 4 bytes)?

Is the underlying data structure of queues and stream buffers the same though?

Also, I can’t seem to find `configSUPPORT_DYNAMIC_ALLOCATION` inside FreeRTOSConfig.h. At the top of the file, it says

```auto
/*
 * FreeRTOS Kernel V10.0.0
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

```

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 1, 2021, 1:51am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/4 "2021-06-01T01:51:47Z")

</div>

For FreeRTOSConfig.h, that is a user created file, so you add whatever definitions you want to it.

The data structures for Queues and StreamBuffers are similar. The actual data-storage part is basically just an array to hold the bytes, and variables keeping track of the current read and write pointers. Queue have a more complicated data structure to handle things needed for the multiple readers and writers, so it can build up a list of task blocked on it. StreamBuffers don’t need that as they can have only a single reader and writer (which makes them more efficient for that case). Queue also need to keep track of their block size, which is always 1 from a StreamBuffer.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 2:20am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/5 "2021-06-01T02:20:23Z")

</div>

I added `#define configSUPPORT_DYNAMIC_ALLOCATION 1` in FreeRTOSConfig.h but the second line still remains “highlighted” and I can’t call `xQueueCreate()`  
[as shown here](https://ibb.co/m9VV38t)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [June 1, 2021, 2:33am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/6 "2021-06-01T02:33:27Z")

</div>

I would first double check that you don’t have two versions of FreeRTOSConfig.h in your project/include path.

Note also, that by default, configSUPPORT\_DYNAMIC\_ALLOCATION is set to 1 if it doesn’t appear in the file.

Sometimes I will put something that is an intentional error, or a #error directive to make sure that I am in the file that is being used.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 4:51am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/8 "2021-06-01T04:51:57Z")

</div>

Yes, but even if configSUPPORT\_DYNAMIC\_ALLOCATION is by default set to 1, how come it’s not being recognized in queue.h to enable the usage of `xQueueCreate()`?

Also I notice that queue.h doesn’t include FreeRTOS.h, rather the source file does it. How does the file know what’s inside FreeRTOSConfig.h?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 1, 2021, 5:06am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/9 "2021-06-01T05:06:35Z")

</div>

If you just use `xQueueCreate` in your code does it compile ?  
Or do you mean not recognized by the IDE ? That’s a question for the IDE provider.  
However, the IDE usually parses the code on it’s own. If it doesn’t parse `FreeRTOS.h` (correctly) in the provided FreeRTOS `include` directory it can’t know the final set of defines and macros.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 5:10am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/10 "2021-06-01T05:10:51Z")

</div>

The build does recognize FreeRTOSConfig.h (added some garbage values and it errored out). Regardless, the default value is set to 1 so I don’t see why the `#define xQueueCreate()` would be “greyed out”. Calling `xQueueCreate` errors out yes.

```auto
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
	#define xQueueCreate( uxQueueLength, uxItemSize ) xQueueGenericCreate( ( uxQueueLength ), ( uxItemSize ), ( queueQUEUE_TYPE_BASE ) )
#endif

```

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 1, 2021, 5:14am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/11 "2021-06-01T05:14:22Z")

</div>

In your application code where using FreeRTOS API you need to include `FreeRTOS.h` first. It includes `FreeRTOSConfig.h` internally.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 5:15am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/12 "2021-06-01T05:15:27Z")

</div>

I am already including FreeRTOS.h inside the header file

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 1, 2021, 5:26am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/13 "2021-06-01T05:26:02Z")

</div>

If the includes and definitions are correct the build should work as in any other FreeRTOS application out there. Maybe verify a demo application as reference how it’s done.  
Sometimes it was reported that people had to struggle with an other (2nd) `FreeRTOSConfig.h` hanging around somewhere which was included unexpectedly.  
Temporarily patching some `#warning` or `#error` directives into the files would help to track the the correct include files and hierarchy and also the definitions.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [June 1, 2021, 5:27am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/14 "2021-06-01T05:27:30Z")

</div>

Does it matter which FreeRTOSConfig.h is included given `configSUPPORT_DYNAMIC_ALLOCATION` is enabled by default?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 1, 2021, 5:30am UTC](https://forums.freertos.org/t/stream-buffers-vs-message-queues/12739/15 "2021-06-01T05:30:01Z")

</div>

If one of your `FreeRTOSConfig.h` set it to 0 … yes. Do yourself a favor and use a _single_ configuration.
