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. Multi-threaded graphics

Multi-threaded graphics

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicsannouncement
10 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.
  • I Offline
    I Offline
    Iceman
    wrote on last edited by
    #1

    Using VC7/MFC I have a dialog app that has a seperate thread to handle animation. How can I do a lock so that I can update a back buffer(double-buffering) between the main thread and the graphics thread. When I try to lock in the main dialog it freezes which I think is being caused by the message pump being stopped by the lock in the thread. Any ideas as to the proper (and hopefully easy) way to handle multi-thread screen updating? Thanks

    I J M 3 Replies Last reply
    0
    • I Iceman

      Using VC7/MFC I have a dialog app that has a seperate thread to handle animation. How can I do a lock so that I can update a back buffer(double-buffering) between the main thread and the graphics thread. When I try to lock in the main dialog it freezes which I think is being caused by the message pump being stopped by the lock in the thread. Any ideas as to the proper (and hopefully easy) way to handle multi-thread screen updating? Thanks

      I Offline
      I Offline
      Iceman
      wrote on last edited by
      #2

      One more thing...is MsgWaitForMultipleObjects the proper way to do this?

      1 Reply Last reply
      0
      • I Iceman

        Using VC7/MFC I have a dialog app that has a seperate thread to handle animation. How can I do a lock so that I can update a back buffer(double-buffering) between the main thread and the graphics thread. When I try to lock in the main dialog it freezes which I think is being caused by the message pump being stopped by the lock in the thread. Any ideas as to the proper (and hopefully easy) way to handle multi-thread screen updating? Thanks

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        Off the top of my head, put a critical section in both the back buffer drawing routines and the main drawing routine. Another common method is to create two back buffers and ping pong between them using a BYTE index (which is always atomic) to indicate which buffer is active for painting in the foreground and drawing in the background.

        I 1 Reply Last reply
        0
        • J Joe Woodbury

          Off the top of my head, put a critical section in both the back buffer drawing routines and the main drawing routine. Another common method is to create two back buffers and ping pong between them using a BYTE index (which is always atomic) to indicate which buffer is active for painting in the foreground and drawing in the background.

          I Offline
          I Offline
          Iceman
          wrote on last edited by
          #4

          Tried the critical section but when it tries to lock in the main thread, everything hangs for some reason. I think the message pump may be getting blocked. Used critical sections a lot in my server code and they work great. For some reason it doesn't seem to want to cooperate with graphics routines.

          J 1 Reply Last reply
          0
          • I Iceman

            Tried the critical section but when it tries to lock in the main thread, everything hangs for some reason. I think the message pump may be getting blocked. Used critical sections a lot in my server code and they work great. For some reason it doesn't seem to want to cooperate with graphics routines.

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            I'm going to guess you have one path in the thread which doesn't release the critical section. If that doesn't work, try running around your office screaming and waving your arms in the air.:)

            I 1 Reply Last reply
            0
            • J Joe Woodbury

              I'm going to guess you have one path in the thread which doesn't release the critical section. If that doesn't work, try running around your office screaming and waving your arms in the air.:)

              I Offline
              I Offline
              Iceman
              wrote on last edited by
              #6

              lol tried both. The screaming worked better than the critical section. I checked all the paths, only use it in one place in each thread. Had that happen before so it was the first thing I thought to check. Very perplexing problem.

              G 1 Reply Last reply
              0
              • I Iceman

                Using VC7/MFC I have a dialog app that has a seperate thread to handle animation. How can I do a lock so that I can update a back buffer(double-buffering) between the main thread and the graphics thread. When I try to lock in the main dialog it freezes which I think is being caused by the message pump being stopped by the lock in the thread. Any ideas as to the proper (and hopefully easy) way to handle multi-thread screen updating? Thanks

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

                Messages for a window can be processed only by the thread that created the window. That's how message pumps work. Designing you code so that another thread updates the UI will likely result in other problems (or at the very least, inefficiencies when the threads have to wait on each other). But sticking with your question, if the worker thread is the only one touching the back buffer, why do you need a lock at all? --Mike--    THERE IS NO     THERE IS NO    BUT THERE IS MAGIC PIXIE DUST  BUSINESS GENIE  CODE PROJECT Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

                I 1 Reply Last reply
                0
                • M Michael Dunn

                  Messages for a window can be processed only by the thread that created the window. That's how message pumps work. Designing you code so that another thread updates the UI will likely result in other problems (or at the very least, inefficiencies when the threads have to wait on each other). But sticking with your question, if the worker thread is the only one touching the back buffer, why do you need a lock at all? --Mike--    THERE IS NO     THERE IS NO    BUT THERE IS MAGIC PIXIE DUST  BUSINESS GENIE  CODE PROJECT Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

                  I Offline
                  I Offline
                  Iceman
                  wrote on last edited by
                  #8

                  Thanks for the info. The back buffer is processed by the paint routine in the main thread as well. What's the best way to handle multi-threaded graphics to a dialog window?

                  I 1 Reply Last reply
                  0
                  • I Iceman

                    Thanks for the info. The back buffer is processed by the paint routine in the main thread as well. What's the best way to handle multi-threaded graphics to a dialog window?

                    I Offline
                    I Offline
                    Iceman
                    wrote on last edited by
                    #9

                    Maybe I can explain it better. Only the main thread that created the dialog processes messages. The worker thread only updates only the back buffer which is then processed by the main thread and updated to the screen.

                    1 Reply Last reply
                    0
                    • I Iceman

                      lol tried both. The screaming worked better than the critical section. I checked all the paths, only use it in one place in each thread. Had that happen before so it was the first thing I thought to check. Very perplexing problem.

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #10

                      Yes, but was it a manly scream or a high-pitched girly scream? I've found that only high-pitched girly screams help with threading problems. Seriously though. Could you post some of your code?


                      Software Zen: delete this;

                      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