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