Click to See Complete Forum and Search --> : Very basic question about C++ threading
stephenprogrammer07
June 28th, 2007, 12:56 PM
HI
In my game, I am working on a part that will probably freeze up the game while it begins. So I would like to launch that bit of code as a separate thread.
In java I know how to launch a new thread.
What is the technique or syntax for launching a new thread in C++ ?
It's really just 1 method that I would want to launch a s a new thread. but i'm guessing that if it's like java it would only be possible to create a new class and make it run as a thread.
What may I ask is the technique or syntax for launching a new thread in C++ ?
And then how do you keep a handle on to it so that you can stop it if it is still alive.
Thanks in Advance
Stev
dinus
June 28th, 2007, 01:51 PM
take a look at _beginthreadex function:
m_hMyThread=(HANDLE)_beginthreadex(NULL, 0, MyThread, this, 0, (unsigned int*)&dwMyThreadID);
Then MyThread function:
unsigned _stdcall MainWndThread(LPVOID lpParam)
{
while(g_bMyThreadRunning)
{
//Put your code here
}
}
Before calling the thread, set g_bMyThreadRunning=TRUE.
To kill the thread, g_bMyThreadRunning=FALSE
Cheers.
MikeAThon
June 28th, 2007, 06:59 PM
See this FAQ: "How to create a worker thread?" at http://www.codeguru.com/forum/showthread.php?t=350759#sdk_thread
Mike
Ejaz
June 29th, 2007, 04:51 AM
In my game, I am working on a part that will probably freeze up the game while it begins. So I would like to launch that bit of code as a separate thread.
Can you explain what you are doing that is freezing the entire game? Usually, multi-threading is avoided in games unless its necessary.
stephenprogrammer07
June 29th, 2007, 01:41 PM
I'm going to add text to speech technology.
Sometimes it seems that the microsoft engine takes a 1 second or 2 before the speech is sent to the audio card. In that break = i don't think i would like all the animation on the screen to freeze .
Arjay
June 30th, 2007, 03:41 PM
I'm going to add text to speech technology.
Sometimes it seems that the microsoft engine takes a 1 second or 2 before the speech is sent to the audio card. In that break = i don't think i would like all the animation on the screen to freeze .Is the present only during the first usage? Or does this happen everytime speech is sent? Also, do you do some processing to form the speech prior to sending it?
stephenprogrammer07
July 2nd, 2007, 09:08 AM
Thanks for responding.
It appears to me that at the point when I past the string to the method to do the speaking , that it takes maybe 1 or 2 seconds to start speaking.
It appears to happen every time the speak method is called.
So I was thinking I should send it as an independent thread just so that on screen animation, player idle animations etc can continue rather than freeze.
Sincerely
Stephen
stephenprogrammer07
July 2nd, 2007, 09:46 AM
I'm using Dev C++ and it does not recognize DWORD or HANDLE.
Where are those imported from.
I tried the following code from the thread FAQ
class CFoo
{
public:
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;
}
private:
DWORD m_dwThreadID;
HANDLE m_hThread;
static DWORD WINAPI ThreadFunc(LPVOID pvParam);
};
Arjay
July 2nd, 2007, 01:09 PM
Open your code in the Visual Studio compiler, double click on 'DWORD', right-click and choose 'go to definition.' This will open the appropriate header file and take you to the spot where it's defined.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.