Posted By

joshuacalloway on 05/24/10


Tagged


Versions (?)

HW 17-1


 / Published in: C++
 

  1. // Joshua Calloway
  2. // cspp51045
  3. // 17-1
  4. //
  5. #include <iostream>
  6. #include <boost/thread/thread.hpp>
  7. #include <boost/thread/mutex.hpp>
  8.  
  9. using namespace std;
  10.  
  11. boost::mutex io_mutex;
  12.  
  13. struct counter
  14. {
  15. counter(int id) : id(id) {}
  16. void operator()() {
  17. for (int i = 0; i < 100; i++) {
  18. boost::thread::yield();
  19. boost::mutex::scoped_lock lock(io_mutex);
  20. cout << "Thread " << id << " been called " << i << " times." << endl;
  21. }
  22. }
  23. int id;
  24. };
  25.  
  26. int main() {
  27. boost::thread_group thrds;
  28.  
  29. thrds.add_thread(new boost::thread(counter(1)));
  30. thrds.add_thread(new boost::thread(counter(2)));
  31. thrds.add_thread(new boost::thread(counter(3)));
  32.  
  33. thrds.join_all();
  34. return 0;
  35. }

Report this snippet  

You need to login to post a comment.