Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/thread.hpp>
- #include <boost/shared_ptr.hpp>
- #include <vector>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- void threadfunc( int param, int loops )
- {
- int j = 0;
- // Following is replaced by real number crunching
- srand( (unsigned)time(NULL) );
- for( int i = 0; i < loops; i++ )
- j += param * rand()*sin(i);
- }
- int main()
- {
- int thread_num = 2;
- int iters = 20;
- int loops = 1000000;
- std::vector< boost::shared_ptr<boost::thread> > workerArray;
- workerArray.resize(thread_num);
- for( int j = 0; j < iters; j++ )
- {
- for( int i = 0; i < thread_num; i++ )
- {
- workerArray[i].reset();
- workerArray[i] = boost::shared_ptr<boost::thread>( new boost::thread( &threadfunc, i, loops )); // quite ugly :(
- }
- // here writing data from previous iteration to file
- for( int i = 0; i < thread_num; i++ )
- workerArray[i].get()->join();
- }
- cout << "Finish!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement