stephenprogrammer07
August 21st, 2007, 01:09 AM
Hi
I have a thread in my code. My thread code is similar to the thread format seen below - but it does a number of things and holds on to critical resources. My thread actually does microsoft speech - each thread says a new phrase.
In testing, I discovered, that I absolutely have to wait until my thread is finished before I launch another thread, seems like there is only 1 source of resources for speech. ( It seems to defeat the purpose a little but , at least I'm able to get a little onscreen animation while the speech is going on. )
I would love to TerminateThread and launch the next thread. But it seems like there is no mechanism to do so safely and free the resources for the next thread. Correct me If I'm Wrong.
I looked at SuspendThread but i need the thread to be gone to make a new one.
So my last option, seems to be to just Wait for the thread to return before I launch a new speech thread.
So my question is , what is the Mechanism that I would use to determine when the Speech Thread (DWORD WINAPI Threadfunc(LPVOID Param) ) has returned ? ?
Thanks in advance
Stephen
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
class CFoo
{
private:
DWORD m_dwThreadID;
HANDLE m_hThread;
static DWORD WINAPI ThreadFunc(LPVOID pvParam);
public:
HANDLE TheHandle(void) const { return m_hThread; }
CFoo()
{
m_dwThreadID = 0;
m_hThread = 0;
}
~CFoo() { CloseHandle(m_hThread); }
bool Create()
{
m_hThread = CreateThread(0,
0,
ThreadFunc,
this,
0,
&m_dwThreadID);
if(!m_hThread)
{
// Could not create thread
return false;
}
return true;
}
};
DWORD WINAPI CFoo::ThreadFunc(LPVOID pvParam)
{
cout << endl << "Start thread" << endl;
// Wait for 10 seconds.
for(int i = 0; i < 100; i++)
{
::Sleep(100);
}
cout << endl << "returning from thread" << endl;
return 0u;
}
int main(int argc, char *argv[])
{
CFoo cf;
cf.Create();
cout << "finished calling cf.Create()" << endl;
return EXIT_SUCCESS;
}
I have a thread in my code. My thread code is similar to the thread format seen below - but it does a number of things and holds on to critical resources. My thread actually does microsoft speech - each thread says a new phrase.
In testing, I discovered, that I absolutely have to wait until my thread is finished before I launch another thread, seems like there is only 1 source of resources for speech. ( It seems to defeat the purpose a little but , at least I'm able to get a little onscreen animation while the speech is going on. )
I would love to TerminateThread and launch the next thread. But it seems like there is no mechanism to do so safely and free the resources for the next thread. Correct me If I'm Wrong.
I looked at SuspendThread but i need the thread to be gone to make a new one.
So my last option, seems to be to just Wait for the thread to return before I launch a new speech thread.
So my question is , what is the Mechanism that I would use to determine when the Speech Thread (DWORD WINAPI Threadfunc(LPVOID Param) ) has returned ? ?
Thanks in advance
Stephen
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
class CFoo
{
private:
DWORD m_dwThreadID;
HANDLE m_hThread;
static DWORD WINAPI ThreadFunc(LPVOID pvParam);
public:
HANDLE TheHandle(void) const { return m_hThread; }
CFoo()
{
m_dwThreadID = 0;
m_hThread = 0;
}
~CFoo() { CloseHandle(m_hThread); }
bool Create()
{
m_hThread = CreateThread(0,
0,
ThreadFunc,
this,
0,
&m_dwThreadID);
if(!m_hThread)
{
// Could not create thread
return false;
}
return true;
}
};
DWORD WINAPI CFoo::ThreadFunc(LPVOID pvParam)
{
cout << endl << "Start thread" << endl;
// Wait for 10 seconds.
for(int i = 0; i < 100; i++)
{
::Sleep(100);
}
cout << endl << "returning from thread" << endl;
return 0u;
}
int main(int argc, char *argv[])
{
CFoo cf;
cf.Create();
cout << "finished calling cf.Create()" << endl;
return EXIT_SUCCESS;
}