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 confusions

thread confusions

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
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.
  • A Offline
    A Offline
    anilaabc
    wrote on last edited by
    #1

    1.when a thread complete its execution ,can we restart its execution using same handle ? 2.when a thread complete its execution is it cosuming resources? 3.I need to do multiple tasks at the same time ,so I need to work in threads.as i have to repeate these tasks at regular interval so i have to start same threads ,time is critical so i m using multimedia timer to start threads at regular interval . problem is I have to create a new thread after a regular interval.I think it will consume resources . How could I use same threads to run at regular interval ????????????????

    S C M 3 Replies Last reply
    0
    • A anilaabc

      1.when a thread complete its execution ,can we restart its execution using same handle ? 2.when a thread complete its execution is it cosuming resources? 3.I need to do multiple tasks at the same time ,so I need to work in threads.as i have to repeate these tasks at regular interval so i have to start same threads ,time is critical so i m using multimedia timer to start threads at regular interval . problem is I have to create a new thread after a regular interval.I think it will consume resources . How could I use same threads to run at regular interval ????????????????

      S Offline
      S Offline
      sashoalm
      wrote on last edited by
      #2

      anilaabc wrote:

      1.when a thread complete its execution ,can we restart its execution using same handle ?

      I don't think so. Once a thread is finished, it's handle should become invalid.

      anilaabc wrote:

      2.when a thread complete its execution is it cosuming resources?

      Yes. Also when it starts its execution. It has to allocate/deallocate its stack, at least.

      anilaabc wrote:

      3.I need to do multiple tasks at the same time ,so I need to work in threads.as i have to repeate these tasks at regular interval so i have to start same threads ,time is critical so i m using multimedia timer to start threads at regular interval . problem is I have to create a new thread after a regular interval.I think it will consume resources . How could I use same threads to run at regular interval ????????????????

      AFAIK there's no simple way to do that. You can create a thread that waits for a message or an event, and then executes a task in response to that message (or event). But you have to take care of the communication mechanism yourself.

      There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

      1 Reply Last reply
      0
      • A anilaabc

        1.when a thread complete its execution ,can we restart its execution using same handle ? 2.when a thread complete its execution is it cosuming resources? 3.I need to do multiple tasks at the same time ,so I need to work in threads.as i have to repeate these tasks at regular interval so i have to start same threads ,time is critical so i m using multimedia timer to start threads at regular interval . problem is I have to create a new thread after a regular interval.I think it will consume resources . How could I use same threads to run at regular interval ????????????????

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        1. AFAIK no 2. After it terminated -unless it leaks memory or GDI resources- no. 3. I think what you could do is to use 1 or 2 events do control your thread (or threads), so your thread routine would look something like this:

        HANDLE Events[] = {TerminateEvent, RepeatEvent};
        BOOL Continue = TRUE;
        while (Continue)
        {

        //Perform whatever your thread needs to perform at this point here...

        switch (WaitForMultipleObjects(2, Events, FALSE, INFINITE))
        {
        case WAIT_OBJECT_0: //The terminate event has been signalled, so break the loop, do cleanup, exit
        Continue = FALSE;
        break;
        case WAIT_OBJECT_0 + 1: //The repeat event has been signalled, do whatever we need to do again
        continue;
        default: //Unexpected event, do whatever is necesarry here...
        Continue = FALSE;
        break;
        }
        }

        The TherminateEvent and RepeatEvent you would create using the CreateEvent API call and signal them from another thread. You would probably need to add some more synchronization too. Does this help?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        1 Reply Last reply
        0
        • A anilaabc

          1.when a thread complete its execution ,can we restart its execution using same handle ? 2.when a thread complete its execution is it cosuming resources? 3.I need to do multiple tasks at the same time ,so I need to work in threads.as i have to repeate these tasks at regular interval so i have to start same threads ,time is critical so i m using multimedia timer to start threads at regular interval . problem is I have to create a new thread after a regular interval.I think it will consume resources . How could I use same threads to run at regular interval ????????????????

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          anilaabc wrote:

          1.when a thread complete its execution ,can we restart its execution using same handle ?

          No.

          anilaabc wrote:

          2.when a thread complete its execution is it cosuming resources?

          Yes. From the docs: "The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle"

          anilaabc wrote:

          i m using multimedia timer to start threads at regular interval

          Is there a reason you can't just use the timer thread? You may want to look into creating a pool of threads so you don't have to create and destroy them rapidly.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            anilaabc wrote:

            1.when a thread complete its execution ,can we restart its execution using same handle ?

            No.

            anilaabc wrote:

            2.when a thread complete its execution is it cosuming resources?

            Yes. From the docs: "The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle"

            anilaabc wrote:

            i m using multimedia timer to start threads at regular interval

            Is there a reason you can't just use the timer thread? You may want to look into creating a pool of threads so you don't have to create and destroy them rapidly.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            A Offline
            A Offline
            anilaabc
            wrote on last edited by
            #5

            I could n't understan ...IS timer not execute in the main thread?

            M 1 Reply Last reply
            0
            • A anilaabc

              I could n't understan ...IS timer not execute in the main thread?

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Multimedia timers execute on their own thread.

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                Multimedia timers execute on their own thread.

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                A Offline
                A Offline
                anilaabc
                wrote on last edited by
                #7

                thanks Mark

                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