Can FindFirstFile/FindNextFile be multithread?
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
Yes they can, but don't make changes to the directory structure because they may fail. In your case I would use a thread to get the subdirectories only, then launch another thread to process the files in the directory ( you may want to take a look at thread pooling to do this ). Be very careful about where you are writing your results. Your problem sounds to me like two threads trying to write to the same location.
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
What and where erros?
WhiteSky
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
-
What and where erros?
WhiteSky
Each time when the program find a file, it send it to the file_handler() to process. The file_handler() is a function in where I create another thread to process the file. It is like this:
void CMyDoc1::file_handler(LPCTSRT pFileName) { m_fileName = pFileName; AfxBeginThread(ThreadProc, this); } UINT ThreadProc(PVOID pParam) { //processing the file... CMyDoc1* pDoc = pParam; pDoc ->FileProcessor(); return 0; }
The error occurs in FileProcessor(), when free the memory. -- modified at 6:47 Monday 11th December, 2006 -
Each time when the program find a file, it send it to the file_handler() to process. The file_handler() is a function in where I create another thread to process the file. It is like this:
void CMyDoc1::file_handler(LPCTSRT pFileName) { m_fileName = pFileName; AfxBeginThread(ThreadProc, this); } UINT ThreadProc(PVOID pParam) { //processing the file... CMyDoc1* pDoc = pParam; pDoc ->FileProcessor(); return 0; }
The error occurs in FileProcessor(), when free the memory. -- modified at 6:47 Monday 11th December, 2006rgbalpha wrote:
...when free the memory.
Where?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hello folks, I want to search all of files under some directory. For example, dir\file1.txt, dir\file2.txt, dir\file3.txt, ... When using ::FindFirstFile and FindNextFile, I can get all files, and then I can process them. But because the processing may last long, I want to use multithread, but There are strage errors. Can ::FindFirstFile/FindNextFile be multithread? or Are they thread safe? Thanks in advance. Haifeng
It is not safe to have mutiple threads messing with the same find handle as the same time. It should be safe to first construct the full path to the file and pass it to a thread like you later examples demonstrates, but you have to make sure that the memory pointed to by the filename parameter is valid for as long as the thread needs it. This is the kind of situation where "hand-off" memory can be used, either by allocating a
TCHAR
buffer vianew[]
and passing it to the thread which will later calldelete[]
on it, or passing a string object. For these kinds of situations, you may want to use a pool of threads with something like an I/O Completion Port and throw processing requests at it. That way, you do not create too many threads bogging the system down (you do not want to create 1500 threads if 1500 files need to be processed), and you can adjust the number of threads in the pool depending on the resources available on the target system and/or the performance needs of the application. You can also look into seeing if theQueueUserWorkItem(...)
function is available on the target system and use it to manage the thread pool and the work to be done. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
rgbalpha wrote:
...when free the memory.
Where?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
It is not safe to have mutiple threads messing with the same find handle as the same time. It should be safe to first construct the full path to the file and pass it to a thread like you later examples demonstrates, but you have to make sure that the memory pointed to by the filename parameter is valid for as long as the thread needs it. This is the kind of situation where "hand-off" memory can be used, either by allocating a
TCHAR
buffer vianew[]
and passing it to the thread which will later calldelete[]
on it, or passing a string object. For these kinds of situations, you may want to use a pool of threads with something like an I/O Completion Port and throw processing requests at it. That way, you do not create too many threads bogging the system down (you do not want to create 1500 threads if 1500 files need to be processed), and you can adjust the number of threads in the pool depending on the resources available on the target system and/or the performance needs of the application. You can also look into seeing if theQueueUserWorkItem(...)
function is available on the target system and use it to manage the thread pool and the work to be done. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)