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
questionhelptutorial
5 Posts 3 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
    Ruca
    wrote on last edited by
    #1

    Hi, I have a multi-threaded application and have the following problem, which I am not sure how to solve. I have 2 threads running. The 1 thread I would like to SUSPEND or place in SLEEP mode. This is so that I can give the processor a chance to do other things. This thread I only need to work every half an hour. If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. Also if I should need to kill the thread and it is in sleep, I am going to have to wait a half an hour ( at the most) before the thread dies. How do I manage this? Thanks in advance for any ideas. Regards Rui

    D 1 Reply Last reply
    0
    • R Ruca

      Hi, I have a multi-threaded application and have the following problem, which I am not sure how to solve. I have 2 threads running. The 1 thread I would like to SUSPEND or place in SLEEP mode. This is so that I can give the processor a chance to do other things. This thread I only need to work every half an hour. If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. Also if I should need to kill the thread and it is in sleep, I am going to have to wait a half an hour ( at the most) before the thread dies. How do I manage this? Thanks in advance for any ideas. Regards Rui

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Ruca wrote: If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. You shouldn't be using Sleep for this. You have three choices: 1. The most complicated, but more elegant and powerful: Use WaitForSingleObject and use an event object with the timeout value being the Sleep period 2. I do not recommend, but easier: Use SuspendThread from the thread 2 for suspending thread 1. Then set some bool and ResumeThread for resuming the execution. 3. The most horrible: If the program is terminating, you can use TerminateThread My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

      L 1 Reply Last reply
      0
      • L Le centriste

        You could also use a timer catching the WM_TIMER message. To make the trade terminate, send it the WM_QUIT message, which it will also catch. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
        - TreeBeard

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        He's using Sleep, so he's not processing messages on his thread's message pump... My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

        L 1 Reply Last reply
        0
        • D Daniel Turini

          Ruca wrote: If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. You shouldn't be using Sleep for this. You have three choices: 1. The most complicated, but more elegant and powerful: Use WaitForSingleObject and use an event object with the timeout value being the Sleep period 2. I do not recommend, but easier: Use SuspendThread from the thread 2 for suspending thread 1. Then set some bool and ResumeThread for resuming the execution. 3. The most horrible: If the program is terminating, you can use TerminateThread My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          You could also use a timer catching the WM_TIMER message. To make the trade terminate, send it the WM_QUIT message, which it will also catch. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
          - TreeBeard

          D 1 Reply Last reply
          0
          • D Daniel Turini

            He's using Sleep, so he's not processing messages on his thread's message pump... My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            The suggestion is NOT to use Sleep, because it is blocking, so you are unable to tell the thread to cleanup and quit. The use of either WaitForSingleObject with a 1800000 milliseconds time-out or SetTimer and catching the WM_TIMER and WM_QUIT messages is better. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
            - TreeBeard

            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