简体   繁体   English

pthread不等待互斥锁线程完成

[英]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 : %s\n",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 %d\n",iTimeOut,threadRunning);
    while(iTimeOut!=0 && !threadFinished)
    {
        printf("waiting %d\n",++i);
        sleep(1);
        iTimeOut--;
        printf("changing the threadfinish varible\n");
        //pthread_mutex_lock(&g_mutex); //statement 1
        threadFinished = 1;
        //pthread_mutex_unlock(&g_mutex); // statement 2
        printf("changed the threadfinish varible\n");
    }

    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 :  %s\n",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. 当对statement1和statement2进行注释时,我期望子线程先在主线程之前更改我的testrunning变量,但是只有在对statement1和statement2取消注释时,子线程才能正常工作。
My question is why in child thread mutex lock is not locking my testrunning variable . 我的问题是,为什么在子线程互斥锁中没有锁定我的testrunning变量。 it is allowing main thread to modify testrunning variable. 它允许主线程修改testrunning变量。

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. main()线程无法执行此操作。

To correct this you could change main() 's while-loop like this: 要更正此问题,您可以像这样更改main()的while循环:

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

      if (finished)
      {
        break;
      }
    }

    printf("waiting %d\n",++i);

    sleep(1);
    iTimeOut--;

    printf("changing the threadfinish varible\n");

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

    printf("changed the threadfinish varible\n");
  }

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 : %s\n",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. 您应该在所有库调用中添加错误检查,以防故障影响剩余的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM