Advertisement
Guest User

Untitled

a guest
Mar 25th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <boost/thread.hpp>
  2. #include <boost/shared_ptr.hpp>
  3. #include <vector>
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. void threadfunc( int param, int loops )
  12. {
  13.     int j = 0;
  14.  
  15.     // Following is replaced by real number crunching
  16.  
  17.     srand( (unsigned)time(NULL) );
  18.     for( int i = 0; i < loops; i++ )
  19.         j += param * rand()*sin(i);
  20. }
  21.  
  22. int main()
  23. {
  24.     int thread_num = 2;
  25.     int iters = 20;
  26.     int loops = 1000000;
  27.  
  28.     std::vector< boost::shared_ptr<boost::thread> > workerArray;
  29.     workerArray.resize(thread_num);
  30.     for( int j = 0; j < iters; j++ )
  31.     {
  32.         for( int i = 0; i < thread_num; i++ )
  33.         {
  34.             workerArray[i].reset();
  35.             workerArray[i] = boost::shared_ptr<boost::thread>( new boost::thread( &threadfunc, i, loops ));     // quite ugly :(
  36.         }
  37.  
  38.         // here writing data from previous iteration to file
  39.  
  40.         for( int i = 0; i < thread_num; i++ )
  41.             workerArray[i].get()->join();
  42.     }
  43.    
  44.     cout << "Finish!\n";
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement