Details
-
Suggestion
-
Resolution: Invalid
-
Not Evaluated
-
4.4.0
-
None
Description
I noticed that this function actually uses pthread_cond_timedwait to do the sleeping. This is not necessary because there is a pthread_mutex_timedlock which has exactly the same behavior in this case. The use of timedlock would save the initialization and destruction of the pthread_cond in a function that may be called very frequently (think about usleep).
This is how the modified thread_sleep could look like:
static void thread_sleep(struct timespec *ti)
{
pthread_mutex_t mtx;
pthread_mutex_init(&mtx, 0);
pthread_mutex_lock(&mtx);
pthread_mutex_timedlock( &mtx, ti);
pthread_mutex_unlock(&mtx);
pthread_mutex_destroy(&mtx);
}
Notice there is now pthread_cond any more. Seems like a nice and small improvement.
diff of corelib/thread/qthread_unix.cpp:
296,297d295
< pthread_cond_t cnd;
<
299,300d296
< pthread_cond_init(&cnd, 0);
<
302,303d297
< (void) pthread_cond_timedwait(&cnd, &mtx, ti);
< pthread_mutex_unlock(&mtx);
305c299,301
< pthread_cond_destroy(&cnd);
—
> pthread_mutex_timedlock( &mtx, ti);
>
> pthread_mutex_unlock(&mtx);