Getting Two Multi Thread Function To Run At The Same Time
OK first off I just wanted to thank every one who helped me with my problem before. I have one more problem with multi thread and it is I read throught the MSDN and cant seem to find how to make multi thread functions to fun at the same time. I know how to make them run but it doesnt run while another one is running. The source code is::
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
bool Quit = 1;
bool IsFighting = 0;
bool FightInput(char input[40]);
class Player
{
};
void Input()
{
char input[40];
do
{
cin.getline(input,40);
FightInput(input);
}while(Quit);
}
void Moo()
{
int dog = 0;
do
{
if(dog==30000000)
{
cout << "Hello" << endl;
dog = 0;
}
else
dog += 1;
}while(1);
}
void main()
{
HANDLE FightHandle;
DWORD nFight;
FightHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Moo, NULL, 0,&nFight);
WaitForSingleObject(FightHandle, INFINITE);
HANDLE InputHandle;
DWORD nInput;
InputHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Input, NULL, 0,&nInput);
WaitForSingleObject(InputHandle, INFINITE);
}
bool FightInput(char input[40])
{
if((input[0] == ''f'' || input[0] == ''F'') && input[1]== '' '')
{
// Send Who Is Going To Fight
cout << "Your Fighting" << endl;
}
else if(input[1] == ''i'' || input[1] == ''I'')
{
if(input[2] == ''g'' || input[2] == ''G'')
{
}
else
return 1;
if(input[3] == ''h'' || input[3] == ''H'')
{
}
else
return 1;
if(input[4] == ''t'' || input[4] == ''T'')
{
cout << "Your Fighting" << endl;
}
else
return 1;
}
else
return 1;
if(!stricmp(input,"kill")||!stricmp(input,"k"))
{
}
}
I know the code is bad but it was just to test the problem I dont think you really have to read any of the functions at all to get my problem to work. I know it has to do with this set of code.
HANDLE FightHandle;
DWORD nFight;
FightHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Moo, NULL, 0,&nFight);
WaitForSingleObject(FightHandle, INFINITE);
HANDLE InputHandle;
DWORD nInput;
InputHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Input, NULL, 0,&nInput);
WaitForSingleObject(InputHandle, INFINITE);
it is the WaitForSingleObject that is messing me up cause its waiting for the other one to finish so it can create and start the other function. Whould anyone happen to know what is the name of the function or how I would make both of these functions threads run at the same time.
void main()
{
HANDLE FightHandle;
DWORD nFight;
FightHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Moo, NULL, 0,&nFight);
WaitForSingleObject(FightHandle, INFINITE);
HANDLE InputHandle;
DWORD nInput;
InputHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Input, NULL, 0,&nInput);
WaitForSingleObject(InputHandle, INFINITE);
}
The red line shouldn''t be there. It is causing your program to wait until that thread is finished before it starts the next thread, so they don''t run at the same time, and you might as well have just called the functions in the first place.
Instead, you could change the green line to WaitForMultipleObjects() and then it would wait for both of them to finish at that point.
That''s one way to do it anyway.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement