Skip to content

Instantly share code, notes, and snippets.

@wtfbbqhax
Created October 21, 2016 17:22
Show Gist options
  • Save wtfbbqhax/6ac208d6b9f47c7d71439c8f6e63d3e1 to your computer and use it in GitHub Desktop.
Save wtfbbqhax/6ac208d6b9f47c7d71439c8f6e63d3e1 to your computer and use it in GitHub Desktop.
Autoupdate Demo
#include <chrono>
#include <iostream>
#include <fstream>
#include <thread>
#include <unistd.h> // getcwd
#include <libgen.h> // basename
#define VERSION "1.3"
#define MAXPATHLEN 1024
std::string get_working_path()
{
char temp[MAXPATHLEN];
return ( getcwd(temp, MAXPATHLEN) ? std::string( temp ) : std::string("") );
}
int main(int argc, char* argv[])
{
bool running = true;
std::mutex running_mutex;
std::cout << "Version: " << VERSION << "\n";
auto t = std::thread([&]() {
std::string binpath(get_working_path() + '/' + basename(argv[0]));
std::cout << "opening: " << binpath << "\n";
// Wait for the main loop to settle
running_mutex.lock();
running = false;
running_mutex.unlock();
std::fstream fs;
fs.open(binpath, std::fstream::out|std::fstream::trunc);
fs << "Test";
fs.close();
});
for ( ;; )
{
running_mutex.lock();
if ( !running )
{
std::cout << "terminating\n";
running_mutex.unlock();
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
running_mutex.unlock();
}
t.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment