Can i use fork.. join / fork .. join_any inside of a function

Hi,

Can i use fork…join / fork…join_any inside of function?

Example:1 → Getting compilation error.

module top;  
  
  initial begin    
    $display("Before calling of function t1() is %t", $time);
    t1(); 
    $display("After calling of function t1() is %t", $time);
  end

  function void t1();
    fork
      begin : a1
        $display("a1: %t",$time); // **this thread is executing at zero simulation time.**
      end
      begin : a2
        $display("a2: %t",$time); // **this thread is executing at zero simulation time.**
      end
      begin: a3
        $display("a3: %t",$time); // **this thread is executing at zero simulation time.**
      end
    join
  endfunction
endmodule

Example:2 → Getting compilation error

module top; 
  
  initial begin    
    $display("Before calling of function t1() is %t", $time);
    t1(); 
    $display("After calling of function t1() is %t", $time);
  end

  function void t1();
    fork
      begin : a1
        $display("a1: %t",$time); // **this thread is executing at zero simulation time.**
      end
      begin : a2
        #1;
        $display("a2: %t",$time); 
      end
      begin: a3
        #2;
        $display("a3: %t",$time); 
      end
    join_any
  endfunction
endmodule

May i know what is the reason for compilation error even though function with zero simulation of time?

Thanks and Regards,
koti

In reply to pkoti0583:

from LRM:

“A function shall not contain any time-controlled statements. That is, any statements containing #,##, @, fork-join, fork-join_any, wait, wait_order, or expect…”

note that join_none is allowed :p …
from LRM:
“Within a function, a fork-join_none construct may contain any statements that are legal within a task”
i.e even @statement is allowed inside a function, provided it is within join_none…

In reply to pkoti0583:
hi…
In example one perfectly working.
In example two you use a delay inside a function,So it will through a error.
Only use delay in task can’t use in function.Because a function is zero simulation time.

In reply to Rajaraman Rak7:

could be just that your simulator is supporting it - but not right as per LRM
which means when you change simulator it might fail…

In reply to pkoti0583:

The rule for a SystemVerilog function is this:

A function may not block, and may not contain any statement that could potentially block the function.

The compiler is not required to analyze your code to see if your particular situation it would not block.

In any case, a fork/join/join_any that consumes 0 time is of no use.