Refactored gtk thread related functions

All functions are now included in a single class. An object of this
class can be used as a context manager to start/kill the gtk thread as
needed.