Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-2530

[4.4.0] Suggestion: simplify function thread_sleep in qthread_unix.cpp

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Invalid
    • Not Evaluated
    • Some future release
    • 4.4.0
    • Core: Threads
    • 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);

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bhughes Bradley T. Hughes (closed Nokia Identity) (Inactive)
            admin Administrator
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes