Search code examples
cgtkgnomeautosave

Implementing autosave w/o disruption


I have been hacking on the code for Xournal in order to add auto-save functionality. My initial implementation was very dumb: every 60 or so seconds, do a save. The feature ostensibly works.

However, after testing it out for a while, I've noticed that when auto-save runs, the application temporarily freezes up, which is quite annoying if you're in the middle of a pen stroke. I'm trying to figure out how to fix this.

One possibility I've thought up of is for autosave to check whether or not the pen is touching the screen before autosaving; if it is touched, attach a one time only callback scheduled for a second after the pen is lifted. (if the same thing happens, repeat). Another possibility would be to optimize the save function sufficiently such that there is no gap (seems unlikely).

Anyone have any suggestions about this? Xournal uses the Gnome/GTK toolkit, and is written in C.

Update: I implemented the anti-clobber logic, and I'm very happy with the resulting autosave granularity and performance. One of those times threads are (thankfully) not necessary! Thanks all for your suggestions.


Solution

  • If the UI freezes for any noticeable period of time, a separate thread is probably the way to go. If the only reason you're noticing the UI freeze is because you happen to be writing at the time and the interruption is only very brief, then your method might work. Your fix is probably way easier than creating another thread, so try that first.

    If you do end up using threads, go with g_threads instead of pthreads since you're using GTK+. They'll be more portable.