Implemented thread locking.

Fixes #258. CodeMaid didn't clean up header files, so there are some
formatting changes to app.h here too.