2013-03-02 47 views
0

我創建了一個線程。主要功能是創建一個元素並將其附加到隊列的尾部/尾部。線程正在從Head/Start中讀取列表,並在之後釋放內存。segfault在由線程釋放鏈接列表(隊列)中的內存期間

我有以下代碼:

#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <unistd.h> 

void *print_message_function(void *ptr); 

typedef struct stCheckFree 
{ 
    char name[30]; 
    int doneflag; 
    struct stCheckFree *next; 
}CheckFree; 

CheckFree *gHead=NULL; 
CheckFree *gTail=NULL; 

int main() 
{ 
    pthread_t thread1; 
    char *message1 = "Thread 1"; 
    int iret1; 
    unsigned long TestCount=1; 

    CheckFree *pCurr=NULL; 
    CheckFree *pTemp=NULL; 

    iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1); 

    while(1) 
    { 
     pCurr=malloc(sizeof(CheckFree)); 
     memset(pCurr,0,sizeof(CheckFree)); 

     printf("Malloc\n"); 
     sprintf(pCurr->name,"Test-%ld",TestCount); TestCount++; 
     pCurr->doneflag=0; 
     pCurr->next=NULL; 

     pTemp=gTail; 
     gTail=pCurr; 
     if(pTemp) pTemp->next=gTail; 

     if(!gHead) 
     { 
      gHead=gTail; 
     } 

    } 

    return 0; 
} 

void *print_message_function(void *ptr) 
{ 
    CheckFree *pTrav; 

    while(1) 
    { 
     pTrav=gHead; 
     if(pTrav) 
     { 
      printf("[%s]\n",pTrav->name); 
      pTrav->doneflag=1; 

      gHead=gHead->next; 
      free(pTrav); 
     } 
    } 
} 

當我運行它給了我一個段錯誤的代碼。什麼可能是這個問題? 請幫忙!

謝謝。

PS-如果我刪除免費(),它的出色運行!

--------------------------------------------- --------------------------------------
---編輯1 ---
---------------------------------------------- -------------------------------------

我不確定這是不是該修復程序,但我需要更多來自其他stackoverflow成員的輸入。

void *print_message_function(void *ptr) 
{ 
    CheckFree *pTrav; 

    while(1) 
    { 
     pTrav=gHead; 
     if(pTrav) 
     { 
      printf("[%s]\n",pTrav->name); 
      pTrav->doneflag=1; 

      gHead=gHead->next; 

      if(!gHead) gTail=NULL;    /* NEW CODE */ 

      free(pTrav); 
      sleep(0.7); 
     } 
    } 
} 

請幫助,因爲它很重要! :) 再次感謝。

--------------------------------------------- --------------------------------------
---編輯2 ---
---------------------------------------------- -------------------------------------

代碼更改: - if (!gHead)gTail = NULL;/* NEW CODE */ 正在通過重新初始化NULL來銷燬數據。

您可以通過只是在做下面的代碼更改看到它:

...
PTEMP = gTail;
gTail = pCurr;
if(pTemp)pTemp-> next = gTail;如果(!gTail)printf(「數據丟失\ n」);
如果(!gHead)
{
...

請幫我解決這個問題,...

--------------------------------------------- --------------------------------------
---編輯3 ---
---------------------------------------------- -------------------------------------
以下@ wazy推薦使用互斥鎖我更新了代碼如下: -

#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <unistd.h> 

void * thread_function(void *ptr); 

typedef struct stCheckFree 
{ 
    char name[30]; 
    int doneflag; 
    struct stCheckFree *next; 
}CheckFree; 

pthread_mutex_t lock; // EDIT 3 

CheckFree *gHead=NULL; 
CheckFree *gTail=NULL; 

int main() 
{ 
    pthread_t thread1; 
    char *message1 = "Thread 1"; 
    int iret1; 
    unsigned long TestCount=1; 

    CheckFree *pCurr=NULL; 
    CheckFree *pTemp=NULL; 

    if (pthread_mutex_init(&lock, NULL) != 0) // EDIT 3 
    { 
     printf("\n mutex init failed\n"); 
     return 1; 
    } 

    iret1 = pthread_create(&thread1, NULL, thread_function, (void*) message1); 

    while(1) 
    { 
     pCurr=malloc(sizeof(CheckFree)); 
     memset(pCurr,0,sizeof(CheckFree)); 

     sprintf(pCurr->name,"Test-%ld",TestCount); TestCount++; 
     pCurr->doneflag=0; 
     pCurr->next=NULL; 

     pTemp=gTail; 
     gTail=pCurr; 
     if(pTemp) pTemp->next=gTail; 


     //pthread_mutex_lock(&lock); // EDIT 3(commented out) 
     if(!gHead) 
     { 
      pthread_mutex_lock(&lock); // EDIT 4 
      gHead=gTail; 
      pthread_mutex_unlock(&lock); // EDIT 4 
     } 
     //pthread_mutex_unlock(&lock); // EDIT 3(commented out) 
    } 

    pthread_join(thread1, NULL); 
    printf("Thread 1 returns: %d\n",iret1); 

    return 0; 
} 

void * thread_function(void *ptr) 
{ 
    CheckFree *pTrav; 

    while(1) 
    { 
     pTrav=gHead; 
     if(pTrav) 
     { 
      //printf("[%s]\n",pTrav->name); 
      pTrav->doneflag=1; 

      gHead=gHead->next; 
      if(!gHead) sleep(1);//gTail=NULL; 
      free(pTrav); 
     } 
    } 
} 

我在正確的軌道上????? 謝謝!

+0

新行解決了崩潰,但它創造了另一個bug。由於這條線我失去了數據。 請幫忙! – 2013-03-02 19:17:57

回答

1

運行你的代碼給我免費或glibc腐敗(fasttop)。看看你的代碼,我發現你在線程1和主線程中都使用了gHead。這似乎是一個多線程同步問題。

+0

是的!究竟!!!你能否指導我到一個正確的錯誤免費的快樂的地方,我可以malloc和免費的!謝謝!!! :) – 2013-03-02 20:14:45

+0

請搜索術語互斥。 – wazy 2013-03-02 20:32:35

+0

我把互斥鎖在main()或線程函數? – 2013-03-02 20:45:31