Hi,
I encountered this question in an interview.
“What happens if you call a task inside a function in a fork join_none?”
So if the task has delay, function can just run it in a fork join_none and continue on its execution.
what happens in this case, will the task run.
Please answer.
In reply to jaswanth_b:
Hello if you call a task inside a function and that specific task has some sort of delay execution (event, waits or @ something) then you will be getting errors because you are trying to use delays or sync methods in a function that are illegal (at compile time).
module tb;
initial begin
fork
call_task(15);
call_task(10);
join_none
#1;
$finish();
end
function void call_task(input int a);
$display("FROM FUNCTION");
print(a);
endfunction
task print(input int v);
$display("FROM TASK");
// Events, waits or other constructs are illegal here and should result in error
// you will get warnings because it could bring delays
// #1; // -> error
$display("VALUE: %d",v);
endtask
endmodule
In reply to Rsignori92:
In reply to jaswanth_b:
Hello if you call a task inside a function and that specific task has some sort of delay execution (event, waits or @ something) then you will be getting errors because you are trying to use delays or sync methods in a function that are illegal (at compile time).
module tb;
initial begin
fork
call_task(15);
call_task(10);
join_none
#1;
$finish();
end
function void call_task(input int a);
$display("FROM FUNCTION");
print(a);
endfunction
task print(input int v);
$display("FROM TASK");
// Events, waits or other constructs are illegal here and should result in error
// you will get warnings because it could bring delays
// #1; // -> error
$display("VALUE: %d",v);
endtask
endmodule
I believe the question is about calling tasks inside a function using fork-join_none which is described in the LRM 13.4.4 Background processes spawned by function calls
module tb;
initial begin
fork
call_task(15);
call_task(10);
join_none
#2;
$finish();
end
function void call_task(input int a);
$display("FROM FUNCTION");
fork //LRM 13.4.4 Background processes spawned by function calls
print(a);
join_none
endfunction
task print(input int v);
$display("FROM TASK at %0t", $realtime);
#1;
$display("VALUE: %d at %0t",v , $realtime);
endtask
endmodule
So this example the tasks will be spawned by the call_task function.
For example in questa I get the following output
VSIM 1> run -a
FROM FUNCTION
FROM TASK at 0
FROM FUNCTION
FROM TASK at 0
VALUE: 10 at 1
VALUE: 10 at 1
But you may need be careful with spawning threads and not checking them.
HTH,
-R
In reply to rgarcia07:
Thanks for the answer
by the way the second print statement must print
VALUE: 15 at 1 (instead of 10 ) right as you are passing 15 from function.
In reply to jaswanth_b:
10-10 is the right result, this is a different story since the functions/tasks have static lifetime in module so there is no stack call. Hence they share the same memory 15 and then assigned to 10 that means only 10 printed 2.
declare them as automatic and you will see 15 coming.
Regards
In reply to jaswanth_b:
A function cannot consume time so it’s a trap for most candidates during an interview. we can call a task inside a function if the task is wrapped by a fork join_none as the thread (i.e task) will not get executed until it encounters the next blocking assignment. So the overall compilation still satisfies the basic definition of a function not consuming any time.