Example code for implementing threads with automatic timeouts