06-13-2011 07:33 PM
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
06-13-2011 08:13 PM
06-13-2011 08:57 PM
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.
06-14-2011 10:14 AM
ok, i found it ! it's actually quiet simple... !
you can find attached the solution if anyone cares!
Thank you anyways
06-14-2011 10:36 AM
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. 🙂
10-17-2015 01:22 PM - edited 10-17-2015 01:22 PM
Hi altenbach,
What if we do need to remove the item from the queue? Any ideas on how to go about doing this?
Thanks
10-17-2015 02:20 PM
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
,
10-17-2015 07:09 PM
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.
10-17-2015 10:34 PM
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
10-20-2015 08:29 AM
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!