1. 程式人生 > 其它 >日常記錄(28)雜項、xx

日常記錄(28)雜項、xx

fork join的label(tag)

https://bbs.eetop.cn/thread-883536-1-1.html

其中只要有任何一個執行緒結束,都退出並行執行塊,並列印DONE。要求分別用fork-join、fork-join_any,fork-join_none來實現.

實現基於label的方法會非常容易。由於label本身範圍更廣,比disable fork控制的更容易(線上程裡寫disable fork就沒有意義),所以直接實現了。

至於使用process類,簡單好用,不會誤傷到同名的執行緒,但是實際測試過程中,如果是同一個time-solt,那可能會都執行了,我程式碼的問題?

module taa ();
    initial begin
        fork:thread_label
            begin
                #2 $display("a thread1");
                disable thread_label;
            end
            begin
                #3 $display("a thread2");
                disable thread_label;
            end
            begin
                #4 $display("a thread3");
                disable thread_label;
            end
        join

        $display("this is the end of fork join");
        #10 ;
    end


    initial begin
        fork
            begin
                #1 $display("a fork any thread1");
            end
            begin
                $display("a fork any thread2");
            end
            begin
                $display("a fork any thread3");
            end
        join_any

        $display("this is the end of fork any");
        disable fork;
        #10 ;
    end

    initial begin
        event e1;
        fork:thread_label3
            begin
                #1 $display("a fork none thread_label3 thread1");
                disable thread_label3;
            end
            begin
                $display("a fork none thread_label3 thread2");
                disable thread_label3;
            end
            begin
                $display("a fork none thread_label3 thread3");
                disable thread_label3;
            end
        join_none

        wait fork;
        $display("this is the end of fork none");
        #10 ;

    end

    initial begin
        process jobs[]=new[3];

        fork 
            begin
                jobs[0]=process::self;
                #13 $display("a fork in thread1");
            end
            begin
                jobs[1]=process::self;
                #12 $display("a fork in thread2");
            end
            begin
                jobs[2]=process::self;
                #12 $display("a fork in thread3");
            end
        join_none

        foreach(jobs[i]) begin
            wait(jobs[i]!=null);
        end

        fork 
            jobs[0].await();
            jobs[1].await();
            jobs[2].await();
        join_any

        foreach (jobs[j]) begin
            if(jobs[j].status != process::FINISHED )
                jobs[j].kill();
        end

        $display("the end of process");
        #10;

    end
endmodule

  

輸出

a fork any thread2
this is the end of fork any
a fork none thread_label3 thread2
this is the end of fork none
a thread1
this is the end of fork join
a fork in thread2
a fork in thread3
the end of process

  

本文來自部落格園,作者:大浪淘沙、,轉載請註明原文連結:https://www.cnblogs.com/bai2022/p/15757251.html