日常记录(26)fork join

fork join

`include "exp_inside/tbb.sv"
class ClassBase;

    virtual function void disp();
        $display("the base print");
    endfunction: disp

endclass : ClassBase

class ClassEx1 extends ClassBase;

    function void disp();
        $display("the ex1 print");
    endfunction: disp

endclass : ClassEx1

class ClassEx2 extends ClassEx1;

    function void disp();
        $display("the ex2 print");
    endfunction: disp

    extern function int calc();

endclass : ClassEx2

function int ClassEx2::calc();
    $display("this is a calc");
    return 10;
endfunction

module taa ();
    initial begin
        ClassBase cb=new;
        ClassBase ce1=new;
        ClassEx2 ce2=new;
        cb=ce2;
        cb.disp;
        ce1=ce2;
        ce1.disp;
        $display("ce2.calc() ans %d", ce2.calc());
    end

    initial begin
        event e1;
        ForkJoin fj1=new(e1);
        fj1.fork_join;
        #100
        fj1.wait_and_disable;
        #100
        fj1.event_access;
        -> e1;
    end
endmodule

  

class ForkJoin;
    // data or class properties
    event e1;
    // initialization
    function new(event e1);
        this.e1=e1;
    endfunction : new

    task event_access();
        $display("event wait.");
        fork
            @e1;
            $display("event e1 is traggerd");
        join

        $display("pass");
    endtask: event_access

    task wait_and_disable();
        fork
            #1 $display("this is a fork in wait_and_disable");
        join

        fork
            #2 $display("this is a fork in #2");
        join

        fork
            #1 $display("#1 in wait ");
        join

        $display("before");
        wait fork;
        $display("wait finished");

        fork
            #10 $display("#10");
            #1 $display("#1");
        join_none

        disable fork;
        $display("disable finished");
        
    endtask: wait_and_disable

    task automatic fork_join();

        fork
            $display("thread1 start");
            #10 $display("this is a 10 in fork join");
            #5 $display("this is a 5 in fork join");
        join

        fork
            #6 $display("this is a 6 in fork join_any");
            #3 $display("this is a 3 in fork join_any");
        join_any
        #0 $display("parent in #0 print");
        $display("this is a parent thread in 0");

        fork
            #0 $display("thread3 start");
            #1 $display("thread3 in #1 ");
        join_none

        #1 $display("this is a p2 in #1");
        #0 $display("this is a #0 in parent thread");
    endtask: fork_join

endclass

输出

the ex2 print
the ex2 print
this is a calc
ce2.calc() ans          10
thread1 start
this is a 5 in fork join
this is a 10 in fork join
this is a 3 in fork join_any
parent in #0 print
this is a parent thread in 0
thread3 start
this is a p2 in #1
thread3 in #1 
this is a #0 in parent thread
this is a 6 in fork join_any
this is a fork in wait_and_disable
this is a fork in #2
#1 in wait 
before
wait finished
disable finished
event wait.
event e1 is traggerd

  

 不要考验ForkJoin

        fork
            $display("a");
        join_none
        $display("b");
        fork
            #0
            #1 $display("a");
        join_none
        #1 
        #0 $display("b");


        fork
            #1 $display("aa");
        join_none
        #1 $display("bb");

输出

b
a
a
b
bb
aa

  

上一篇:面试必备之手写节流和防抖函数


下一篇:运维调试记录:Ubuntu启动到字符界面和图形界面