• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. Fork a task with parameter from within a function

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 64
  • Views 1600
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Fork a task with parameter from within a function

Jeff000
Jeff000 over 7 years ago

Hi,

Below is what I've tried to start a task with parameter from a function.


function fork_task_in_func()
	for (int i = O; i < 3; i++) begin
		fork
		  print_me(i);
		join_none
	end
endfunction

task print_me(int i);
	$display("print me: i: %Od\n", i);
endtask

What I expect is as follows,

print me: i: 0
print me: i: 1
print me: i: 2

But the actual outputs are,

print me: i: 3
print me: i: 3
print me: i: 3

Anyone can explain a bit for this result?

Thanks.

  • Cancel
  • StephenH
    StephenH over 7 years ago

    The forked threads are not started until the join_none is reached, meaning that when the threads start the for loop has finished and i == 3. The LRM explains this in some detail, and shows the correct way to do it using an automatic variable. This automatic variable works because the initialiser takes effect instantly rather than waiting till the thread starts.

    for (int i=0; i<3; i++) begin : loop

    fork

    automatic int ii = i;

    begin

    print_me(ii);

    end

    join_none

    end : loop

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Jeff000
    Jeff000 over 7 years ago in reply to StephenH

    Thanks.

    If the forked threads won't get started until the end of the for loop, then the automatic ii will still be assigned with the i == 3, right?

    I've tried as your suggestion but still have the following output,

    print me: i: 3
    print me: i: 3
    print me: i: 3

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • StephenH
    StephenH over 7 years ago in reply to Jeff000

    My apologies, I replied before my morning coffee and got the automatic variable in the wrong place, I've updated my post to show the correct code. See also the end of section 9.3.2 in the 2012 LRM.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information