LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

need to take the N-th element from a queue

hello everyone!

 

I have a problem, i don't know how to take ony one element from a queue...

Indeed, i need to take only the 3rd element of a queue but i don't know how to do that... so i though maybe i can change it as an array (with the vi "flush queue") and then take the 3rd element of this new array but same problem, i don't know how to manage that!

please help me, i have been trying to solve this problem all day long 😞

 

thank you very much!

 

Yanis

0 Kudos
Message 1 of 12
(5,464 Views)
Should the third element disappear from the queue when you read it, moving all higher element down by one slot?
0 Kudos
Message 2 of 12
(5,462 Views)

I am not sure if there is a more elegant solution but what I have done in the past in similar situations was to get all elements of the queue with the queue status. Then I would flush the queue and pop any elements I wanted back on the queue while removing the items I needed/wanted. As I say, this is not the most elegant but it is a tried and true method. If your elements are fairly small and you don't have lots of elements on the queue the performance hit is not too terrible.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 3 of 12
(5,457 Views)

ok, i found it ! it's actually quiet simple... !

you can find attached the solution if anyone cares! 

 

Thank you anyways

0 Kudos
Message 4 of 12
(5,424 Views)

Well, this actually leaves the queue intact and does not take (as in "remove") the particular element from the queue. This missing detail was the reason for my question above. 🙂

0 Kudos
Message 5 of 12
(5,414 Views)

Hi altenbach,

 

What if we do need to remove the item from the queue? Any ideas on how to go about doing this?

 

Thanks

CLA
0 Kudos
Message 6 of 12
(5,181 Views)

El Diego,

     If you were paying attention, Mark told you one way to do this.  If you think about it, to "remove an element from a queue", you need to (pardon me for stating the obvious) need to remove (at least one, maybe more) element(s) from the Queue.  There are two ways to do this -- Flush Queue (which removes them all), and DeQueue (which removes the oldest element).  Once you get (enough) elements off to be able to find the element you need and remove it, you can rebuild the Queue by putting the remaining elements back.

 

     Here's an interesting problem or consideration -- what if another process is modifying the Queue as you are playing with it?  Normally, multiple VIs are "allowed" to write to the Queue, but only a single VI should be allowed to remove from the Queue.  Presumably this will still be true, but now instead of removing the "1st oldest", you'll want to remove the "third oldest" -- that should be OK (provided the Queue has at least three elements), but what if other VIs or loops are adding to the Queue while you are rebuilding it?  Can you think of a "safe" way to rebuild it that is (more-or-less) guaranteed to preserve the original order in the face of new asynchronous additions?  (Hint -- don't do a normal Enqueue ...).

 

     So my answer to the question "Remove third oldest element" might be to say "Go Pop, Pop, Pop, Use, Push, Push" (which, I just realized, will even work if there are originally only two elements on the Queue!).  I'll Leave As an Exercise to the Reader to deduce what Pop and Push mean.

 

Bob Schor

0 Kudos
Message 7 of 12
(5,152 Views)

Bob,

 

I'm assuming you are suggesting he enqueue at the opposite end rather than the typical enqueue.  How would this prevent problems you'd run into with other processes writing to the queue?  If you're sending messages, I'm assuming you have SOME high priority message elsewhere that could be sent.  This would happen regardless of which way we rebuild the queue here.

 

The solution won't be elegant.  Honestly, I'm a bit curious as to what it is you're trying to do.  It's strange that you'd want to pull only the third element from the queue and leave the first two elements in place.  It's also strange that you'd know exactly which element you want but couldn't just enqueue it on the opposite end to make it be the first element to be dequeued.  I expect if we understand what you're trying to do, we can give you a better way to do it.

0 Kudos
Message 8 of 12
(5,125 Views)

Yeah, it's really not something you can do really safely.  Probably better to rethink the entire process, maybe have two queues, or something similar to that.  Your question ("Why do you want to do that?") is a better approach ...

 

BS

0 Kudos
Message 9 of 12
(5,104 Views)

Hi Bob and natasftw,

 

I am writing an application with a Main vi that communicates with parallel vi's, each with its own queue (queued state machines). Each queue is made up of a cluster of an enum (states) and a variant (data), similar to what Anthony_L suggested in his document on the NI Community forum (https://decibel.ni.com/content/docs/DOC-32964).The reason why I was trying to take (remove) some item from the queue was because these items have priority over everything else (from a specific parallel vi to the main). I'm trying to mimic a priority queue, but I am far too deep into this application to modify the whole structure. If there is a simple way to do so, please let me know. I would appreciate any feedback you guys might have.

 

Thanks!

CLA
0 Kudos
Message 10 of 12
(5,047 Views)