I want to call a function with different value of arg many times. To make the execution faster I want to call the function without considering its implementation.
Inside a loop i pass the arg to the function and call it now my program should not wait for the function to execute and loop again, instead it should not consider that the function has not completed for the first loop it should go ahead and call the function again and again till the loop ends.
how to use fork and threads to do this , which one will be faster and a skeleton of code will help ? Lets say my function is void foo(arg1,arg2)
If it's a computationally intensive method then it's not a good idea to spawn a thread/process for each call (as a matter of fact, it's never a good idea!).
You can efficiently parallelize the for loop using openmp:
This will create a number of threads according to the number of available CPU cores and then split the iterations among them. You can further tune the scheduling using the
schedule
clause.You want an asynchronous implementation of the proactor pattern.
Here's an example, using
boost::asio
andboost::thread
:The benefit here, is you can scale easily by calling
boost::asio::io_service::run
from a pool of threads if necessary.Like Tudor has recommend, I would also use a
parallel_for
pattern algorithm from either Intel TBB or Microsoft PPL.If you really want to spawn a need thread for every function, you can do it like this: