Image

Imagejdevelop wrote in Imagecpp

Thread::sleep

Hello, is there something in Posix threads, which might be used to suspend thread for some time (sleep)?
I checked the things and learned that only pthread_cond_timedwait is offered by pthread.h, but it's not exactly what i need, moreover - it fails sometimes and drops my application in core. So I still using the sleep from unistd.h.

I wrote some subroutine in my PosixThread wrapper, but sometimes it fails.

virtual void sleep(int t_out)
{
	pthread_cond_t stub = PTHREAD_COND_INITIALIZER;
	struct timespec timeout;
	struct timeval  now;
	pthread_mutex_t *mutex = new  pthread_mutex_t;
	gettimeofday(&now,NULL);
	timeout.tv_sec=now.tv_sec + t_out;
	timeout.tv_nsec = now.tv_usec * 1000;
	pthread_cond_timedwait(&stub, mutex, &timeout);
}



Is it correct, or I missing something?