I have a perl object with a few functions in it. Each functions is called once from the main program. I would like to run some of the functions in parallel to save time. I can't run all of them together since some functions depend on the results of previous functions.
I thought of something like this:
- For each function keep a flag that is initialized to false and is set to true by the function when it ends (e.g. the last line in
func1
would be$is_func1_done = 1
). Start each function with a loop that waits until all the flags of the functions it depends on are true. For example: if
func1
depends onfunc2
andfunc3
then:sub func1 { while (!($is_func2_done && $is_func3_done)) { # do nothing } # do work }
Then I can start immediately a thread for each function, but the actual work of each function will start only when it's ready. Does this make sense? Do I need any locks here on the flags? Is using such while
loops common? -- the term busy waiting comes to mind... maybe most of my CPU time will be spent on these while
s? Is there a more standard solution to this?