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. Threads

Threads

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 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.
  • B Offline
    B Offline
    baerten
    wrote on last edited by
    #1

    Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object

    M A D 3 Replies Last reply
    0
    • B baerten

      Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Remember when I said "When messages are sent between threads, it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded"? Guess what's happening in your app...

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      B 1 Reply Last reply
      0
      • M Michael Dunn

        Remember when I said "When messages are sent between threads, it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded"? Guess what's happening in your app...

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        B Offline
        B Offline
        baerten
        wrote on last edited by
        #3

        Is the best way to let the Frames & Views in the standard thread and create only for the "hard working functions" a Worker-Thread ? Thanks

        R 1 Reply Last reply
        0
        • B baerten

          Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object

          A Offline
          A Offline
          Aqueel
          wrote on last edited by
          #4

          I am afraid, i can not accommodate 100000000000. :sigh:

          We Believe in Excellence www.aqueelmirza.cjb.net

          1 Reply Last reply
          0
          • B baerten

            Is the best way to let the Frames & Views in the standard thread and create only for the "hard working functions" a Worker-Thread ? Thanks

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

            baerten wrote:

            Is the best way to let the Frames & Views in the standard thread and create only for the "hard working functions" a Worker-Thread ?

            To put it short: Yes! The most common reason to use worker threads is to keep the GUI responsive during operations that will take a lot of time. The task is handed over to a thread that usually posts(!) a message to the main thread when it's done. The main thread is the one that also handles the GUI. The use of worker threads for solving other kind of problems should be carefully considered in order to prevent design flaws and performance bottlenecks. The most common mistake related to multithreading is the use of ::SendMessage() between threads. As Mike said "it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded". This will cause a deadlock, it's just a matter of time. The remedy for this is to use ::PostMessage() instead. This is also the reason that the GUI shall never be manipulated from a secondary thread using MFC since those calls will result in a call to ::SendMessage(). Keep in mind that multithreading is not the same as real multitasking. My point is that if you post a message to a thread, the message will not be handled until the receiving thread is given a time slice to run in by the OS. This means that you have to expect a latency before action is taken on the request that is represented by the posted message. Nowdays you can assume this time to be about 10ms under normal circumstances. For Win95 it was about 55ms. Regarding your initial post there might be other design flaws or bugs that are not related to the use of multiple threads. Aqueel's post points out one. Even if your variable could hold a value that big, you can expect your application to freeze and the GUI to be non-responsive since your burning a lot of MIPS in a UI-handler for a button click event. Hope this helps -- 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

            1 Reply Last reply
            0
            • B baerten

              Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object

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

              Before you get to deep into threads, and unable to surface, read here and here.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              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