Ticket #11166: fsremoverace.cc

File fsremoverace.cc, 922 bytes (added by Jeff Epler <jepler@…>, 8 years ago)

demonstrate race in boost::filesystem::remove

Line 
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
8using namespace boost::filesystem;
9
10boost::condition_variable cond;
11boost::mutex mut;
12
13#define FNAME ("remove-test")
14void remover()
15{
16 while(1)
17 {
18 boost::filesystem::remove(FNAME);
19 }
20}
21
22void creater()
23{
24 for(int i=0; i<100000; i++) std::fstream(FNAME, std::fstream::out);
25}
26
27int 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}