Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can FindFirstFile/FindNextFile be multithread?

Can FindFirstFile/FindNextFile be multithread?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rgbalpha
    wrote on last edited by
    #1

    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

    N W H S J 5 Replies Last reply
    0
    • R rgbalpha

      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

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      rgbalpha wrote:

      There are strage errors

      what errors?

      nave

      1 Reply Last reply
      0
      • R rgbalpha

        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

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R rgbalpha

          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

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          What and where erros?


          WhiteSky


          R 1 Reply Last reply
          0
          • R rgbalpha

            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

            S Offline
            S Offline
            Sam Hobbs
            wrote on last edited by
            #5

            Multithreading can easily be a problem. It requires a lot of thought. It is highly likely that the problem has nothing to do with FindFirstFile/FindNextFile and therefore it is unlikely you have provided enough information to allow anyone to help.

            1 Reply Last reply
            0
            • H Hamid Taebi

              What and where erros?


              WhiteSky


              R Offline
              R Offline
              rgbalpha
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • R rgbalpha

                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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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

                R 1 Reply Last reply
                0
                • R rgbalpha

                  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

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #8

                  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 via new[] and passing it to the thread which will later call delete[] 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 the QueueUserWorkItem(...) 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!)

                  R 1 Reply Last reply
                  0
                  • D David Crow

                    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

                    R Offline
                    R Offline
                    rgbalpha
                    wrote on last edited by
                    #9

                    In FielProcessor() { new pFile = File(); delete pFile; // Error when there are nearly 25 threads. The first serval threads are OK. }

                    1 Reply Last reply
                    0
                    • J James R Twine

                      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 via new[] and passing it to the thread which will later call delete[] 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 the QueueUserWorkItem(...) 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!)

                      R Offline
                      R Offline
                      rgbalpha
                      wrote on last edited by
                      #10

                      Hi, Thank you for the solution. I'll have a try this method. Best Regards, Haifeng

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups