Title

SystemVerilog -- 3.5 Wait fork

Wait fork

wait fork允许 main thread 等待,直到所有 forked threads 都结束。这在 main thread 必须生成多个 threads 并在等待所有 threads 完成之前执行某些功能的情况下非常有用。

Example

我们将使用上一篇文章中相同的示例,其中 3 个 threads 并行启动,main thread 等待其中一个 thread 完成。main thread 恢复后,让我们等到所有 forked threads 都完成。

module tb_top;
  initial begin
    // Fork off 3 sub-threads in parallel and the currently executing main thread will finish when any of the 3 sub-threads have finished.
    fork
      // Thread1 : Will finish first at time 40ns
      #40 $display ("[%0t ns] Show #40 $display statement", $time);

      // Thread2 : Will finish at time 70ns
      begin
        #20 $display ("[%0t ns] Show #20 $display statement", $time);
        #50 $display ("[%0t ns] Show #50 $display statement", $time);
      end

      // Thread3 : Will finish at time 60ns
      #60 $display ("[%0t ns] TIMEOUT", $time);
    join_any

    // Display as soon as the fork is done
    $display ("[%0t ns] Fork join is done, wait fork to end", $time);

    // Wait until all forked processes are over and display
    wait fork;
    $display ("[%0t ns] Fork join is over", $time);
  end
endmdoule

模拟日志

ncsim> run
[20 ns] Show #20 $display statement
[40 ns] Show #40 $display statement
[40 ns] Fork join is done, wait fork to end
[60 ns] TIMEOUT
[70 ns] Show #50 $display statement
[70 ns] Fork join is over
ncsim: *W,RNQUIE: Simulation is complete.

Dose wait fork wait until all processes are over ?

为了了解它在这种情况下的行为,让我们在forked two threads 并等待 forked 完成。

module tb_top;
  initial begin
    // Fork off 3 sub-threads in parallel and the currently executing main thread will finish when any of the 3 sub-threads have finished.
    fork
      // Thread1 : Will finish first at time 40ns
      #40 $display ("[%0t ns] Show #40 $display statement", $time);

      // Thread2 : Will finish at time 70ns
      begin
        #20 $display ("[%0t ns] Show #20 $display statement", $time);
        #50 $display ("[%0t ns] Show #50 $display statement", $time);
      end

      // Thread3 : Will finish at time 60ns
      #60 $display ("[%0t ns] TIMEOUT", $time);
    join_any

    // Display as soon as the fork is done
    $display ("[%0t ns] Fork join is done, wait fork to end", $time);

    // Fork two more processes
    fork
      #10 $display ("[%0t ns] Wait for 10", $time);
      #20 $display ("[%0t ns] Wait for 20", $time);
    join_any

    // Wait until all forked processes are over and display
    wait fork;
    $display ("[%0t ns] Fork join is over", $time);
  end
endmdoule

模拟日志

ncsim> run
[20 ns] Show #20 $display statement
[40 ns] Show #40 $display statement
[40 ns] Fork join is done, wait fork to end
[50 ns] Wait for 10
[60 ns] TIMEOUT
[60 ns] Wait for 20
[70 ns] Fork join is over
ncsim: *W,RNQUIE: Simulation is complete.

posted on 2024-05-12 22:22  松—松  阅读(113)  评论(0编辑  收藏  举报

导航