Multi-threaded for-loop (test my program)
Processors with HyperThreading are becoming more and more common, and processors with multiple cores for home users are not very far away.
With these processors, dividing the work over multiple threads is much more efficient than doing all the work in a single thread.
Take a look at this code:
Code:
for (int i=0; i < 10000; ++i)
func(i);
What if the compiler could automatically split up for-loops over multiple threads?
I've created a custom for-loop that does exactly that; it divides the work over multiple threads. Here's an example:
Code:
#include "Petter-For.h"
using Petter::For;
#include <boost/lambda.hpp>
using namespace boost::lambda;
boost::lambda::placeholder1_type i;
...
For<int>( i=0, i < 1000, i++, cout << i << ' ');
This will print the number from 1 to 1000, using multiple threads (2 by default). An extra template argument can be passed to the function, specifying the number of threads to use.
You will need an iostream library that is thread-safe.
If you have time, I'd like to know what you think about this early version of the library. It is cross-platform, but you will need the boost library installed.