日常记录(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

  

上一篇:1723. 完成所有工作的最短时间


下一篇:php 实现简单的自定义计划任务组件 crontab-jobs