thread confusions
-
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 ????????????????
-
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 ????????????????
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.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 ????????????????
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.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 ????????????????
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:
-
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:
-
Multimedia timers execute on their own thread.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Multimedia timers execute on their own thread.
Mark Salsbery Microsoft MVP - Visual C++ :java: