Skip to content
Advertisement

pthread not waiting for mutex lock threadFinished

Hi below is my coding snippet

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define TIMEOUT 3
int threadFinished = 0;
pthread_mutex_t g_mutex;

void *threadFunction(void* attr)
{
    pthread_mutex_lock(&g_mutex);
    char *pData = (char*)attr;
    if(pData)
    {
        printf("data from main thread is : %sn",pData);
    }
    sleep(10);
    threadFinished = 1;
    pthread_mutex_unlock(&g_mutex);
    return (void*)"This is thread message !";
}

int main()
{
    pthread_t tid;
    char *retVal = NULL;
    int iTimeOut = TIMEOUT;
    int i =0;
    pthread_mutex_init(&g_mutex,NULL);
    pthread_create(&tid,NULL,threadFunction,"Message from main thread");

    //printf("itimeout %d , threadrunning %dn",iTimeOut,threadRunning);
    while(iTimeOut!=0 && !threadFinished)
    {
        printf("waiting %dn",++i);
        sleep(1);
        iTimeOut--;
        printf("changing the threadfinish variblen");
        //pthread_mutex_lock(&g_mutex); //statement 1
        threadFinished = 1;
        //pthread_mutex_unlock(&g_mutex); // statement 2
        printf("changed the threadfinish variblen");
    }

    if(iTimeOut==0)
    {
        if(!threadFinished)
        {
            printf("Timed out so cancelling the thread n");
            pthread_cancel(tid);
        }
        else
        {
            printf("thread finished n");
        }
    }
    else
    {
        printf("thread finished its job n");
        pthread_join(tid,(void*)&retVal);

    }
    pthread_mutex_destroy(&g_mutex);
    threadFinished = 0;
    printf("message from thread is :  %sn",retVal);
    return 0;
}

When statement1 & statement2 are commented I am expecting child thread to change my testrunning variable first before main thread but it is working properly only when statement1 and statement2 are uncommented.
My question is why in child thread mutex lock is not locking my testrunning variable . it is allowing main thread to modify testrunning variable.

Advertisement

Answer

When accessing a variable concurrently from multiple threads, each thread needs to protect access to it via the same mutex. The main() thread fails to do so.

To correct this you could change main()‘s while-loop like this:

  while (iTimeOut != 0)
  {
    {
      pthread_mutex_lock(&g_mutex);
      int finished = threadFinished;
      pthread_mutex_unlock(&g_mutex);

      if (finished)
      {
        break;
      }
    }

    printf("waiting %dn",++i);

    sleep(1);
    iTimeOut--;

    printf("changing the threadfinish variblen");

    pthread_mutex_lock(&g_mutex); //statement 1
    threadFinished = 1;
    pthread_mutex_unlock(&g_mutex); // statement 2

    printf("changed the threadfinish variblen");
  }

Also you might like to consider narrowing locking to where it is necessary inside the thread-function like this:

void *threadFunction(void* attr)
{
  char *pData = attr; /* No need to cast here in C, as opposed to C++. */
  if(pData)
  {
    printf("data from main thread is : %sn",pData);
  }
  sleep(10);

  pthread_mutex_lock(&g_mutex);
  threadFinished = 1;
  pthread_mutex_unlock(&g_mutex);

  return "This is thread message !"; /* No need to cast here in C, as opposed to C++. */
}

You should added error checking to all library calls in case a failure would influence the remaining execution.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement