2012-02-05 63 views
1

我已经通过Linux上的C/C++线程(有一些遗留代码)通过线程单独拷贝了数百万个文件夹。线程同步问题

像扫描文件夹,将文件从源文件复制到目标文件夹等各项功能都已到位,并且在串行执行时可以正常工作。整个问题通过线程完成。

问题:每次执行代码时,代码的行为都会有所不同。有时它会复制整个文件夹列表,但有时会失败。

的样品失败输出是:

foldername is [Junk] tCount is 2 cntr is 3 

val of folder is Junk tid is 3055356816 

foldername is [Notes] tCount is 3 cntr is 4 

val of folder is tid is 3042966416 

Folder creation failed /var/anshul/work/copyDirectoryThreaded/test_copied/email/ 

在功能线程参数的传递的值变为NULL。在上述情况下,参数Notes已从主函数传递到线程函数,但在线程函数中,接收的值为NULL

我主要的代码如下所示:

int main() 
{ 
    int cntr=0; 
    int Val = 3; 

    tCount = 0; 
    pthread_t thread[folderCount]; // folderCount is total number of folders scanned 
    int iret[folderCount]; 
    std::map<std::string,int>::iterator mFolderIt; // mFolder map contains the folder list. 
    char foldername[30] = {0}; 

    for (mFolderIt=mFolder.begin() ; mFolderIt != mFolder.end();) 
    { 
     if(tCount < Val) 
     { 
      pthread_mutex_lock(&mutex_tCount); 
      tCount++; 
      pthread_mutex_unlock(&mutex_tCount); 

      sprintf(foldername,"%s",(*mFolderIt).first.c_str()); 
      fprintf(stderr, "foldername is [%s] tCount is %d cntr is %d\n",foldername,tCount,cntr); 
      iret[cntr] = pthread_create(&thread[cntr], NULL,folderCopy , (void*)foldername); 
      cntr++; 
      usleep(1); // most crucial for threading....... 
      mFolderIt++; 
      memset(foldername,0,30); 
     } 
     else 
     { 
      while(tCount >= Val) 
      { 
       usleep(10); 
      } 
     } 
    } 

    for(int x = 0 ; x<folderCount ;x++) 
     pthread_join(thread[x],NULL); 

    return 1; 
} 

线程函数代码:

void * folderCopy(void *f) 
{ 
    fprintf(stderr,"val of folder is %s tid is %u\n", folder, (unsigned int)pthread_self()); 

    pthread_mutex_lock(&mutex_tCount); 
    tCount--; 
    pthread_mutex_unlock(&mutex_tCount); 
    pthread_exit(NULL); 
    return NULL; 
} 

有人可以帮我解决这个问题。

+0

我建议你使用线程池模式来解决问题。无论如何,在你的代码中,你最好使用std :: condition_variable来同步线程而不是等待循环。 – 2012-02-05 03:39:31

+1

'usleep(1); //对穿线最为关键.......'是一面鲜红的旗帜,对我说:“我是破产!”制作一份'foldername'作为传递给pthread_create的一个拷贝,并在你完成之后将它释放到线程中。 – JimR 2012-02-05 03:58:43

+0

heehe Jim,正是...... !!!它更好地大声喊... :-) – 2012-02-05 04:58:38

回答

3
    fprintf(stderr, "foldername is [%s] tCount is %d cntr is %d\n",foldername,tCount,cntr); 
        iret[cntr] = pthread_create(&thread[cntr], NULL,folderCopy , (void*)foldername); 
        cntr++; 
        usleep(1); // most crucial for threading....... 
        mFolderIt++; 
        memset(foldername,0,30); 

有没有保证,那usleep将足够的时间。相反,你应该确保内存将保持有效,直到其他线程有机会使用它。要做到这一点,最简单的办法是给其他线程它自己的数据副本:

iret[cntr] = pthread_create(&thread[cntr], NULL, folderCopy, strdup(foldername)); 
// ... 

void * folderCopy(void *f) 
{ 
    char *folderName = (char *)f; 
    // do something with folderName 
    free(f); 
    return NULL; 
} 

还有其他的方法,以确保线程已经采取了数据的副本,但是这是最简单的获得对。

+0

谢谢bdonlan ..那工作.. :-) – 2012-02-05 04:18:15

1

作为第四个参数传递的指针不是线程安全的。

pthread_create(&thread[cntr], NULL,folderCopy , (void*)foldername); 

所以,每个线程接收相同的指针,因为主线程,所以没有办法知道任何特定线程看到什么内容overwritting该指针的内容。

您需要为每个线程创建内容的私人副本(然后让线程清理它)。