3

Below function is run by producer threads. This function contains duplicate code.

Had it been a threadless program, I would have created a separate function for the duplicate code and called that function as per need with desired arguments.

What should be done when the duplicate code in inside the thread's function?

//  This function is run by the `Producer` threads.
void *producerThreadFunction (void *arg) {
    Q_UNUSED (arg);

    while (1) {
        pthread_t tId = pthread_self(); qDebug () << "\nProducer: " << tId;

        if (sharedQueueA.length () < 10) {
            qDebug () << "\nQueue A, First check by Producer: " << tId;
            pthread_mutex_lock (&mutexVariable);

            if (sharedQueueA.length () < 10) {
                sharedQueueA.push_back (1);
                qDebug () << "\nPushed by Producer " << tId << ": " << "Length of queue A is: " << sharedQueueA.length ();
            }
            else {
                qDebug () << "\nProducer " << tId << " has no work to do since queue is full, and is now in waiting mode. Length of queue A is: " << sharedQueueA.length ();
                pthread_cond_wait (&conditionVariable, &mutexVariable);
            }

            pthread_mutex_unlock (&mutexVariable);
        }
        else if (sharedQueueB.length () < 10)
        {
            qDebug () << "\nQueue B, First check by Producer: " << tId;
            pthread_mutex_lock (&mutexVariable);

            if (sharedQueueB.length () < 10) {
                sharedQueueB.push_back (1);
                qDebug () << "\nPushed by Producer " << tId << ": " << "Length of queue is: " << sharedQueueB.length ();
            }
            else {
                qDebug () << "\nProducer " << tId << " has no work to do since quque is full, and is now in waiting mode. Length of queue B is: " << sharedQueueB.length ();
                pthread_cond_wait (&conditionVariable, &mutexVariable);
            }

            pthread_mutex_unlock (&mutexVariable);
        }
        else
        {
            qDebug () << "Producer: " << tId << "Both the queues are full. Have to wait!";
        }
    }

    return NULL;
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

1 Answers1

8

There is nothing special with a thread callback function, other than that thread safety has to be considered. You can call any function from inside a thread, given that the function is thread-safe.

So simply create a function and move the duplicate code there.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • so to make that new function thread safe, I have to move the locks as well and wait as well? Please explain. – Aquarius_Girl Dec 22 '15 at 10:06
  • 1
    @TheIndependentAquarius You can either move them to the new function, or surround the function call with them. Whatever makes most sense for your program design. – Lundin Dec 22 '15 at 10:10
  • Oh, so they can be moved to the new function? Thanks. BTW, what about nested functions? Should nested function be used? Will that be beneficial in any way? – Aquarius_Girl Dec 22 '15 at 10:13
  • @TheIndependentAquarius "There is nothing special with a thread callback function, other than that thread safety has to be considered." Use nested functions if it makes sense. – Lundin Dec 22 '15 at 10:18