I need a task to return a queue. How can I do this ? I was able to do this using a function in the following way
typedef bit array_type[$]
function array_type my_function();
…
…
return queue_of_elements;
endfunction : my_function
However, I need to move away from function to a task as I need to have some delay element in that (Delay). But I’m not sure how to make my task return that queue_of_elements.
If the task is going to update the queue before some delay, and another process needs to see the queue before the task returns, then you can use a ref argument.
Could you please explain what do you mean by “If the task is going to update the queue before some delay” we could use a ref argument ?
Why the key word ref could provide some delay here, it appears to me that only difference is that how to copy the q (output means task copy the value when it ends, the ref means we pass the handle of variable inside the task)…
The ref argument does not provide delay, it allows the tasks to consume time and keep the argument values in sync. A better example is passing a clock to a task
task t(ref bit clk);
repeat (10) @(posdege clk) $display("tick-tock");
endtask
Had you passed clk as an input argument, the task would hang as it would never see a change.
If you have a task with no blocking statements, or a function, there really is no point in having ref arguments except as an optimization to prevent the copying of large arrays (which many tools do as an optimization implicitly for you anyways)
Will this work, if i call such a task from a fork join?
i have a task with output argument of type queue, not ref such simple output. i call this task from fork join. size of the queue is zero after fork join exits.
For input, output and inout task arguments, it makes no difference what the datatype of the argument is. The input argument get copied by value upon entering the task, and output arguments get copied when exiting the tasks.
The push_back() method of a queue can only push one element onto the queue at a time. You can either add another nested foreach loop or use an array concatenation