Advertisement
Guest User

Untitled

a guest
May 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class Counter
  2. {
  3. public:
  4. Conuter(const std::string& fileName);
  5. boost::uint16_t getCounter();
  6. private:
  7. tbb::atomic<boost::uint32_t> counter;
  8. std::string counterFileName;
  9. };
  10.  
  11. Counter::Counter(const std::string& fileName) : counter(), counterFileName(fileName)
  12. {
  13. std::string line;
  14. std::ifstream counterFile (fileName.c_str());
  15. if (counterFile.is_open())
  16. {
  17. getline (counterFile, line);
  18. counterFile.close();
  19. }
  20.  
  21. boost::uint32_t temp = std::stoul (line,nullptr,0);
  22. counter = temp;
  23. }
  24.  
  25. boost::uint32_t Counter::getCounter()
  26. {
  27. if (counter > 1000)
  28. {
  29. counter = 0;
  30. }
  31.  
  32. assert( counter < 1000);
  33.  
  34. const boost::uint32_t ret = counter++;
  35.  
  36. if ((counter % 10) == 0)
  37. {
  38. // write the counter back to the file.
  39. std::ofstream file (counterFileName.c_str());
  40. if (file.is_open())
  41. {
  42. myfile << counter;
  43. myfile.close();
  44. }
  45. }
  46. return ret;
  47. }
  48.  
  49. boost::thread t1(&Counter::getCounter, counter);
  50. boost::thread t2(&Counter::getCounter, counter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement