Use C++ thread and mutex instead of the C equivalents

The only issue is the usage of pthread_cancel() since there is no C++ equivalent