0
votes

In freertos documentation they say that There are two ways in which queue behavior could have been implemented:

Queue by copy : Queuing by copy means the data sent to the queue is copied byte for byte into the queue.

Queue by reference : Queuing by reference means the queue only holds pointers to the data sent to the queue, not the data itself.

My question is in the code bellow when i send the struct "CommandData" from task1 to task2 and then change the DataArray field in the struct in the receiving task.does this affect the same field in the sending task.

In other word in this case is it Queuing by copy or Queuing by reference?

typedef struct
   {
     uint8_t *                      ArrayLength;
     uint8_t *                      DataArray;
   }
   FunctionStruct;



    bool Read(uint8_t * Length, uint8_t * AttributeData)
    {
      FunctionStruct              CommandData;
       .... 
       __t_CommandData.ArrayLength = Length;
       __t_CommandData.DataArray   = AttributeData;
      ....
      xQueueSendToBack(x_Queue, &CommandData, 0U)
     .....
    }

thank you

2
@Umaiki your comment does not apply to the question - it's a different matter.linuxfan says Reinstate Monica

2 Answers

2
votes

The description from freeRTOS is pretty clear:
Function protoype:

BaseType_t xQueueSendToBack(QueueHandle_t xQueue,
                            const void * pvItemToQueue,
                            TickType_t xTicksToWait);

pvItemToQueue: A pointer to the item that is to be placed on the queue. The size of the items the queue will hold was defined when the queue was created, so this many bytes will be copied from pvItemToQueue into the queue storage area.

NB: Be careful giving 0U as xTicksToWait parameter.

xTicksToWait: The maximum amount of time the task should block waiting for space to become available on the queue, should it already be full. The call will return immediately if this is set to 0. The time is defined in tick periods so the constant portTICK_PERIOD_MS should be used to convert to real time if this is required.

0
votes

You have confused yourself by using struct that contains pointers. Yes, the struct is passed by copy, but the copy holds the pointers, not the actual data.

Since length is a pointer, if the receiver modifies length it will not result change in the sender's context, but if it modifies *length (the actual data length is pointing at) than the sender's *length will indeed be effected.