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. Thread Colsing Problem

Thread Colsing Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformance
7 Posts 4 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.
  • S Offline
    S Offline
    Sunil Lanke
    wrote on last edited by
    #1

    I have created Thread Using AfxBeginThread, to diaply continous text on dialog bar, while if I close entire application or interrupt the application i gets Memory leakage, please help.

    CPalliniC R 2 Replies Last reply
    0
    • S Sunil Lanke

      I have created Thread Using AfxBeginThread, to diaply continous text on dialog bar, while if I close entire application or interrupt the application i gets Memory leakage, please help.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Maybe posting your code will make our lives easier...:zzz:

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • S Sunil Lanke

        I have created Thread Using AfxBeginThread, to diaply continous text on dialog bar, while if I close entire application or interrupt the application i gets Memory leakage, please help.

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        How do you cancel the thread? This doesn't seem to be a suitable solution for updating text. Consider using a timer, CWnd::SetTimer(), to update the text. -- Roger


        "It's supposed to be hard, otherwise anybody could do it!" - selfquote

        "No one remembers a coward!" - Jan Elfström 1998
        "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

        S 1 Reply Last reply
        0
        • R Roger Stoltz

          How do you cancel the thread? This doesn't seem to be a suitable solution for updating text. Consider using a timer, CWnd::SetTimer(), to update the text. -- Roger


          "It's supposed to be hard, otherwise anybody could do it!" - selfquote

          "No one remembers a coward!" - Jan Elfström 1998
          "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

          S Offline
          S Offline
          spielehelfer
          wrote on last edited by
          #4

          ^^ yep if possible use a timer... otherwise by using a thread: load: ThreadRef = AfxBeginThread(Thread, ¶ms, THREAD_PRIORITY_BELOW_NORMAL, 0, CREATE_SUSPENDED); disable auto delete: ThreadRef->m_bAutoDelete = false; start Thread: ThreadRef->ResumeThread(); then use a global variable to signalize the thread to exit (in Thread func) if(params->pDlg->m_WantExit){ return 1; } and use a own method in the main function to clean up the threads, calld before ending, and also wait till they end. do{ Sleep(1); //jump to thread GetExitCodeThread(ThreadRef->m_hThread,&ExitCode); }while(ExitCode == STILL_ACTIVE); this makes sure that the thread gets canceled correct on end of the main function.

          R 1 Reply Last reply
          0
          • S spielehelfer

            ^^ yep if possible use a timer... otherwise by using a thread: load: ThreadRef = AfxBeginThread(Thread, ¶ms, THREAD_PRIORITY_BELOW_NORMAL, 0, CREATE_SUSPENDED); disable auto delete: ThreadRef->m_bAutoDelete = false; start Thread: ThreadRef->ResumeThread(); then use a global variable to signalize the thread to exit (in Thread func) if(params->pDlg->m_WantExit){ return 1; } and use a own method in the main function to clean up the threads, calld before ending, and also wait till they end. do{ Sleep(1); //jump to thread GetExitCodeThread(ThreadRef->m_hThread,&ExitCode); }while(ExitCode == STILL_ACTIVE); this makes sure that the thread gets canceled correct on end of the main function.

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            I suggested a timer in the same thread since you will eventually get a deadlock situation if you update GUI objects from other threads. The timer solution is also more straight forward. Besides, your thread handling suggestion contains a lot of errors and bad solutions that should not be recommended someone. 1. You left out the information what 'params' really is. Usually you pass the 'this' pointer and call a static member function that converts the void pointer. 2. You set CWinThread::m_bAutoDelete to FALSE without deleting the object returned by AfxBeginThread which will create a memory leak. 3. You're calling GetExitCodeThread in a loop where you think your thread will terminate by a call to ::Sleep(), even if you set the priority of the thread below normal. It will terminate eventually, but your main thread has higher priority. The comment that a call to ::Sleep() will execute your secondary thread is very wrong: there's no guarantee that your thread will be executed. Even the argument to ::Sleep() reveals a lot. The correct way to do it is to wait on the thread handle, CWinThread::m_hThread, with a call to e.g. ::WaitForSingleObject(). For more information about how to do multithreading, read here[^]. -- Roger


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote

            "No one remembers a coward!" - Jan Elfström 1998
            "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

            S 1 Reply Last reply
            0
            • R Roger Stoltz

              I suggested a timer in the same thread since you will eventually get a deadlock situation if you update GUI objects from other threads. The timer solution is also more straight forward. Besides, your thread handling suggestion contains a lot of errors and bad solutions that should not be recommended someone. 1. You left out the information what 'params' really is. Usually you pass the 'this' pointer and call a static member function that converts the void pointer. 2. You set CWinThread::m_bAutoDelete to FALSE without deleting the object returned by AfxBeginThread which will create a memory leak. 3. You're calling GetExitCodeThread in a loop where you think your thread will terminate by a call to ::Sleep(), even if you set the priority of the thread below normal. It will terminate eventually, but your main thread has higher priority. The comment that a call to ::Sleep() will execute your secondary thread is very wrong: there's no guarantee that your thread will be executed. Even the argument to ::Sleep() reveals a lot. The correct way to do it is to wait on the thread handle, CWinThread::m_hThread, with a call to e.g. ::WaitForSingleObject(). For more information about how to do multithreading, read here[^]. -- Roger


              "It's supposed to be hard, otherwise anybody could do it!" - selfquote

              "No one remembers a coward!" - Jan Elfström 1998
              "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

              S Offline
              S Offline
              spielehelfer
              wrote on last edited by
              #6

              Well I left several part out, thats true. - I thought its well known whats able to fill 'params' with - the Threadobject would be deleted after the loop jumps out, what happens when the thread has ended (I didnt write it, ok). - that the Sleep() is no 100% secure statement for a threadchange ist true - to secure the application for deadlocks, there are several locking mechanism possible so secure shared variables. - WaitForSingleObject has the problem that it locks up the application that calls it if used tirect in a window creating code, thats why i used a loop. Well Im not a code guru, dont think Im one and also theres every time someone that knows more than me :)

              R 1 Reply Last reply
              0
              • S spielehelfer

                Well I left several part out, thats true. - I thought its well known whats able to fill 'params' with - the Threadobject would be deleted after the loop jumps out, what happens when the thread has ended (I didnt write it, ok). - that the Sleep() is no 100% secure statement for a threadchange ist true - to secure the application for deadlocks, there are several locking mechanism possible so secure shared variables. - WaitForSingleObject has the problem that it locks up the application that calls it if used tirect in a window creating code, thats why i used a loop. Well Im not a code guru, dont think Im one and also theres every time someone that knows more than me :)

                R Offline
                R Offline
                Roger Stoltz
                wrote on last edited by
                #7

                My first post I suggested an alternative design solution for the OP, since I'm under the impression that he had selected the wrong solution for the problem. You don't paint the walls with a hammer. In my second post I was really trying to help you to avoid troubles in your way of doing multithreaded applications.

                spielehelfer wrote:

                - I thought its well known whats able to fill 'params' with - the Threadobject would be deleted after the loop jumps out, what happens when the thread has ended (I didnt write it, ok).

                Ok, so noted. A word of caution though: don't assume that the person you're trying to help is aware of things you consider 'common knowledge'. In my opinion it's hard enough to help someone while guessing their level of expertise.

                spielehelfer wrote:

                - that the Sleep() is no 100% secure statement for a threadchange ist true

                True, in fact it might be quite the opposite: it's almost 100% wrong to use ::Sleep() in this situation. ::Sleep(0) would make a little more sense but it would still not be quite right. Read the timing section [^] of Joe Newcomer's article and you will know why afterwards.

                spielehelfer wrote:

                - to secure the application for deadlocks, there are several locking mechanism possible so secure shared variables

                :suss: A "deadlock" and "corrupt data due to multiple threads accessing the same data" are two very different problems, but both are related to multithreading. Protecting shared data is often called "thread synchronization". You cannot prevent deadlocks by the use of thread synchronization; you may, however, cause a deadlock by improper use of thread synchronization.

                spielehelfer wrote:

                - WaitForSingleObject has the problem that it locks up the application that calls it if used tirect in a window creating code, thats why i used a loop

                If it's the blocking part you worry about I can tell you that your loop solution is worse. (Calling ::Sleep()!) :-> It really doesn't matter: you should use ::WaitForSingleObject(), or ::WaitForMultipleObjects(), for multiple reasons (the ones below at the top of my head): 1. you can set a timeout 2. t

                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