Fix thread exit and device closing on destruction. Fixes #27.
Thanks to djearl on github for the patch.