簡體   English   中英

pthread不等待互斥鎖線程完成

[英]pthread not waiting for mutex lock threadFinished

您好,以下是我的代碼段

#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;
}

當對statement1和statement2進行注釋時,我期望子線程先在主線程之前更改我的testrunning變量,但是只有在對statement1和statement2取消注釋時,子線程才能正常工作。
我的問題是,為什么在子線程互斥鎖中沒有鎖定我的testrunning變量。 它允許主線程修改testrunning變量。

從多個線程並發訪問變量時, 每個線程都需要保護通過同一互斥鎖對其的訪問。 main()線程無法執行此操作。

要更正此問題,您可以像這樣更改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");
  }

另外,您可能想考慮將鎖定范圍縮小到像這樣的線程函數內部所需的位置:

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++. */
}

您應該在所有庫調用中添加錯誤檢查,以防故障影響剩余的執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM