Home > other >  MATLAB PollableDataQueue get last value
MATLAB PollableDataQueue get last value

Time:12-29

Is it possible to make the a MATLAB PollableDataQueue last-in-first-out (LIFO) instead of first-in-first-out (FIFO)? Alternatively, is there a way to get the last entry in a queue instead of the first one?

See for example, the following code that illustrates the current behavior of PollableDataQueue:

%parpool;

q = parallel.pool.PollableDataQueue;

f = parfeval(@test,1,q);

for jj = 1:4
    poll(q)
    pause(0.25)
end

fetchOutputs(f)
function x0 = test(q)

    x0 = [];
    for jj = 1:20
        x0 = [x0, rand(1)];
        send(q, x0);
        pause(0.01);
        
    end

end

ans =

     []


ans =

    0.7918


ans =

    0.7918    0.1528


ans =

    0.7918    0.1528    0.0854


ans =

  Columns 1 through 12

    0.7918    0.1528    0.0854    0.9955    0.3557    0.1679    0.6713    0.5398    0.6599    0.9344    0.7200    0.4450

  Columns 13 through 20

    0.6622    0.4427    0.9791    0.5114    0.0200    0.6054    0.0479    0.3470

Calling "poll(q)" returns the values added to x0, one by one, in the order in which they are added to the queue (FIFO). Instead, I would like to retrieve the last value in the queue, at the moment in which the moment in which poll(q) is called.

For example, since the code below waits 0.25 seconds between successive calls to "poll(q)", and since values are added to x0 every 0.01 seconds, I would suspect that by the second time "poll(q)" is called, the 25th value of x0 should be already recorded. Yet, the second time I call "poll(q)" I only get the first entry (FIFO instead of LIFO).

CodePudding user response:

You can use a while loop inside the for loop to make the queue empty:

for jj = 1:4
  while q.QueueLength > 0
    x0 = poll(q);
  end
  x0
  pause(0.25)
end
  • Related