26 void* Thread::_service(
void* arg)
29 void* ret = threadFunctor->f(threadFunctor->arg);
34 Thread::Thread(ThreadFunc f,
void* arg) {
36 this->threadFunctor->f = f;
37 this->threadFunctor->arg = arg;
38 pthread_mutex_init(&mut, NULL);
39 pthread_cond_init(&cond, NULL);
44 pthread_mutex_destroy(&mut);
45 pthread_cond_destroy(&cond);
49 if(pthread_join(pthread, NULL)) {
50 throw "Error in join for pthread";
54 void Thread::nSleep(
long nanos) {
55 struct timespec req={0},rem={0};
58 int ret = nanosleep(&req, &rem);
61 struct timespec temp_rem;
62 ret = nanosleep(&req, &temp_rem);
66 void Thread::uSleep(
long micros) {
70 void Thread::mSleep(
long milis) {
74 void Thread::sSleep(
long seconds) {
79 pthread_mutex_lock(&mut);
80 pthread_cond_wait(&cond, &mut);
81 pthread_mutex_unlock(&mut);
84 void Thread::execute() {
85 if(pthread_create(&pthread, NULL, _service, this->threadFunctor)) {
86 throw "Error Creating pthread";
90 void Thread::interrupt() {
91 pthread_mutex_lock(&mut);
92 pthread_cond_broadcast(&cond);
93 pthread_mutex_unlock(&mut);