In Perl, how can I wait for threads to end in parallel?

后端 未结 2 1004
名媛妹妹
名媛妹妹 2021-01-05 01:57

I have a Perl script that launches 2 threads,one for each processor. I need it to wait for a thread to end, if one thread ends a new one is spawned. It seems that the join m

2条回答
  •  一个人的身影
    2021-01-05 02:28

    I think you need to move the code that pulls the next file from the list into the threads themselves.

    So every thread would not just process one file, but continue to process until the list is empty.

    This way, you also save on the overhead of creating new threads all the time.

    Your main thread will then join both of them.

    Of course, this requires synchronization on the list (so that they do not pull the same data). Alternately, you could split the list into two (one for each thread), but that might result in an unlucky distribution.

    (PS: No Perl God, just a humble monk)

提交回复
热议问题