Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <thread>
  5.  
  6. #include <unistd.h> // getcwd
  7. #include <libgen.h> // basename
  8.  
  9. #define VERSION "1.3"
  10.  
  11. #define MAXPATHLEN 1024
  12.  
  13. std::string get_working_path()
  14. {
  15. char temp[MAXPATHLEN];
  16. return ( getcwd(temp, MAXPATHLEN) ? std::string( temp ) : std::string("") );
  17. }
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21. bool running = true;
  22. std::mutex running_mutex;
  23.  
  24. std::cout << "Version: " << VERSION << "\n";
  25.  
  26. auto t = std::thread([&]() {
  27. std::string binpath(get_working_path() + '/' + basename(argv[0]));
  28. std::cout << "opening: " << binpath << "\n";
  29.  
  30. // Wait for the main loop to settle
  31. running_mutex.lock();
  32. running = false;
  33. running_mutex.unlock();
  34.  
  35. std::fstream fs;
  36. fs.open(binpath, std::fstream::out|std::fstream::trunc);
  37. fs << "Test";
  38. fs.close();
  39. });
  40.  
  41. for ( ;; )
  42. {
  43. running_mutex.lock();
  44. if ( !running )
  45. {
  46. std::cout << "terminating\n";
  47. running_mutex.unlock();
  48. break;
  49. }
  50. std::this_thread::sleep_for(std::chrono::seconds(1));
  51. running_mutex.unlock();
  52. }
  53.  
  54. t.join();
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement