views:

48

answers:

1

Hello all,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) = CHECKED;

}
A: 

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.

Anders K.
Context switching between threads is going to be negligible considering that each thread will block waiting on a disk access. As for the read efficiency, I think it depends on the access size. The disk doesn't exactly "take turns;" there are schedulers in the OS and on the controller. If all the files are small (4 KB or less), it would be better to make 50 threads, flood the system with requests, and let the disk scheduler figure out how to service the requests in the order that makes the most sense.
Karmastan
I can agree with the context switch since the time spent in I/O will anyway be much more dominant however I really don't think there would be any benefit of having lots of threads running since they all would be reading from what is essentially a sequential disk. Similar to if you have one cashier it doesn't speed up having 50 queues to that cashier.
Anders K.