This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/BEAGLEBK: Queue Example

Part Number: BEAGLEBK

Tool/software: TI-RTOS

Hi,

I want to use queues in my project and was using the following example as reference at this link:

http://software-dl.ti.com/dsps/dsps_public_sw/sdo_sb/targetcontent/bios/sysbios/6_42_02_29/exports/bios_6_42_02_29/docs/cdoc/ti/sysbios/knl/Queue.html#per-instance_config_parameters

However, I modified this example as per my use case and it looks as this now:

  Queue_Elem *elem;

  for (elem = Queue_head(myQ); 
      elem != (Queue_Elem *)myQ; 
      elem = Queue_next(elem)) {
      ...
  }

Below is a simple example of how to create a Queue, enqueue two elements, and dequeue the elements until the queue is empty:

  #include <xdc/std.h>
  #include <xdc/runtime/System.h>
  
  #include <ti/sysbios/knl/Queue.h>
  
  typedef struct Rec {
      Queue_Elem _elem;
      Int data;
  } Rec;
  
  Int main(Int argc, Char *argv[])
  {
      Queue_Handle q;
      Rec r1;
      Rec* rp;
  
      r1.data = 100;
      
  
  
      // create a Queue instance 'q'
      q = Queue_create(NULL, NULL);
  
  
      // enQ a couple of records
      Queue_enqueue(q, &r1._elem);

//In my app because of some internal process, I change the data field of record and requeue it to queue 
      r1.data =200;
      Queue_enqueue(q, &r1._elem);
  
  
      // deQ the records and print their data values until Q is empty
      while (!Queue_empty(q)) {
          rp = Queue_dequeue(q);
          System_printf("rec: %d\n", rp->data);
      }
  
      System_exit(0);
      return (0);
  }

Now when I run this, the queue never gets empty and it stays in while loop. My use case is that I want to record a variable value multiple times and every time enqueue it. At the end, I want to dequeue all the values. How can I do this with TI-RTOS? 

I hope I am clear with the question.

Regards

Vishav

  • Hi Vishav,

    Once you add an element to the queue, you cannot call Queue_enqueue again on it (unless you dequeue it first).

    Please note that Queueu_enqueue/dequeue are not atomic. The Queue_put/get APIs are.

    Also, it's dangerous to modify the elements when they are on a queue (e.g. r1.data =200;). If multiple threads are accessing the queue, another thread might have already dequeued the element.

    Also note that you must take care when enqueueing a stack variable. If you exit the function, that element could get corrupted.

    Todd