11166-remove-race.cpp 822 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <boost/filesystem.hpp>
  2. #include <boost/thread.hpp>
  3. #include <fstream>
  4. boost::condition_variable cond;
  5. boost::mutex mut;
  6. #define FNAME ("remove-test")
  7. void remover()
  8. {
  9. while(1)
  10. {
  11. boost::filesystem::remove(FNAME);
  12. }
  13. }
  14. void creater()
  15. {
  16. for(int i=0; i<100000; i++) std::fstream(FNAME, std::fstream::out);
  17. }
  18. int main()
  19. {
  20. boost::filesystem::remove(FNAME);
  21. boost::filesystem::remove(FNAME);
  22. std::cout <<
  23. "If you got this far, it's OK to remove a file that doesn't exist\n"
  24. "Now trying with one creator thread and two remover threads.\n"
  25. "This is likely to crash after just a few seconds at most." <<
  26. std::endl;
  27. boost::thread c(creater), r1(remover), r2(remover);
  28. c.join();
  29. r1.interrupt(); r1.join();
  30. r2.interrupt(); r2.join();
  31. }