开发者

How to read large number of files using multiple threads,help me please!

in my application there is a small part of function,in which it will read files to get some information,the number of filecount would be utleast 50,So I thought of implementing threading.Say if the user is giving 50 files,I wanted to separate it as 5 *10, 5 thread should be created,so that each thread can handle 10 files which can speed up the process.And also from the below code you can see that some variables are common.I read some articles about threading and I am aware that only one thread should access a variable/contorl at a me(CCriticalStiuation can be used for that).For me as a beginner,I am finding hard to imlplement what I have learned about threading.Somebody please give me some idea with code shown below..thanks in advance

file read function://
void CMyClass::GetWorkFilesInfo(CStringArray& dataFilesArray,CString* dataFilesB,

                        int* check,DWORD noOfFiles,LPWSTR path)

{
        CString cFilePath;  
    int cIndex =0;
    int exceptionInd = 0;
        wchar_t** filesForWork = new wchar_t*[noOfFiles];
    int tempCheck;
    int localIndex =0;
    for(int index = 0;index < noOfFiles; index++)
    {
        tempCheck = *(check + index);
        if(tempCheck == NOCHECKBOX)
        {
            *(filesForWork+cIndex) = new TCHAR[MAX_PATH];
        wcscpy(*(filesForWork+cIndex),*(dataFilesB +index));
            cIndex++;
        }
        else//CHECKED or UNCHECKED
        {
            dataFilesArray.Add(*(dataFilesB+index));
            *(check + localIndex) = *(check + index);
        localIndex++;

        }

    }
    WorkFiles(&cFilePath,dataFilesArray,filesForWork,
                    path,
                    cIndex);
    dataFilesArray.Add(cFilePath);
    *(check + localIndex) = CHECKE开发者_StackOverflowD;

}


I think you would be better off having just one thread to read all the files. The context switch between the threads together with the synchronization issues is really not worth the price in your example. The hard drive is one resource so imagine all five threads taking turns moving the hard drive read heads to various positions on the hard drive == not very effective.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜