Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Thread

Status
Not open for further replies.

Supaflyfrank

Programmer
Oct 6, 2003
1
0
0
US
I Just Wanted To Know I Changed Mircosoft MultiThread Program Which Was Written In C To Use C++ Functions So I Can Turn This Whole Program In C++ But The Problem Is That After My Program Creates A Thread It Doesnt Run A Certain Function And I Dont Know What The Problem Is This Program Suppose To Run On The Command Prompt And When You Run It And Hit A It Creates A New Thread Which Then Will Cause A Happy Face To Bounce Around The Screen And If You Hit A Again +1 More Happy Face Will Bounce Around The Screen And It Will Do That Till There Is 32 Happy Faces Bounceing Around The Screen. But I Dont Know Why This Program Isnt Working I Hit A And Nothing Happens Can Some One Please Help Me Thank You In Advance.







// BOUNCE.CPP


/* Bounce - Creates a new thread each time the letter 'a' is typed.
* Each thread bounces a happy face of a different color around the screen.
* All threads are terminated when the letter 'Q' is entered.
*
* This program requires the multithread library. For example, compile
* with the following command line:
* CL /MT BOUNCE.C
*/

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

#define MAX_THREADS 32

/* getrandom returns a random number between min and max, which must be in
* integer range.
*/
#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

int main( void ); /* Thread 1: main */
void KbdFunc( void ); /* Keyboard input, thread dispatch */
void ClearScreen( void ); /* Screen clear */
void ShutDown( void ); /* Program shutdown */
void WriteTitle( int ThreadNum ); /* Display title bar information */

DWORD WINAPI BounceThreadProc( LPVOID lpParameter ); /* Threads 2 to n: display */

HANDLE hConsoleOut; /* Handle to the console */
HANDLE hRunMutex; /* &quot;Keep Running&quot; mutex */
HANDLE hScreenMutex; /* &quot;Screen update&quot; mutex */
int ThreadNr; /* Number of threads started */
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; /* Console information */


int main() /* Thread One */
{
/* Get display screen information & clear the screen.*/
hConsoleOut = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo( hConsoleOut, &csbiInfo );
ClearScreen();
WriteTitle( 0 );
/* Create the mutexes and reset thread count. */
hScreenMutex = CreateMutex( NULL, FALSE, NULL ); /* Cleared */
hRunMutex = CreateMutex( NULL, TRUE, NULL ); /* Set */
ThreadNr = 0;

/* Start waiting for keyboard input to dispatch threads or exit. */
KbdFunc();

/* All threads done. Clean up handles. */
CloseHandle( hScreenMutex );
CloseHandle( hRunMutex );
CloseHandle( hConsoleOut );

return 0;
}

void ShutDown( void ) /* Shut down threads */
{
while ( ThreadNr > 0 )
{
/* Tell thread to die and record its death. */
ReleaseMutex( hRunMutex );
ThreadNr--;
}
/* Clean up display when done */
WaitForSingleObject( hScreenMutex, INFINITE );
ClearScreen();
}

void KbdFunc( void ) /* Dispatch and count threads. */
{
int KeyInfo;

do
{
KeyInfo = _getch();
if( tolower( KeyInfo ) == 'a' && ThreadNr < MAX_THREADS )
{
ThreadNr++;
CreateThread(NULL, 0, BounceThreadProc, (LPVOID)ThreadNr, 0, NULL );
WriteTitle( ThreadNr );
}
} while( tolower( KeyInfo ) != 'q' );

ShutDown();
}

DWORD WINAPI BounceThreadProc( LPVOID lpParameter )
{
char MyCell, OldCell;
WORD MyAttrib, OldAttrib;
char BlankCell = 0x20;
COORD Coords, Delta;
COORD Old = {0,0};
DWORD Dummy;

DWORD ThreadNum = (unsigned long)lpParameter;

/* Generate update increments and initial display coordinates. */
srand( ThreadNum * 3 );
Coords.X = getrandom( 0, csbiInfo.dwSize.X - 1 );
Coords.Y = getrandom( 0, csbiInfo.dwSize.Y - 1 );
Delta.X = getrandom( -3, 3 );
Delta.Y = getrandom( -3, 3 );

/* Set up &quot;happy face&quot; & generate color attribute from thread number.*/
if( ThreadNum > 16)
MyCell = 0x01; /* outline face */
else
MyCell = 0x02; /* solid face */
MyAttrib = (unsigned)ThreadNum & 0x0F; /* force black background */

do
{
/* Wait for display to be available, then lock it. */
WaitForSingleObject( hScreenMutex, INFINITE );

/* If we still occupy the old screen position, blank it out. */
ReadConsoleOutputCharacter( hConsoleOut, &OldCell, 1, Old, &Dummy );
ReadConsoleOutputAttribute( hConsoleOut, &OldAttrib, 1, Old, &Dummy );
if (( OldCell == MyCell ) && (OldAttrib == MyAttrib))
WriteConsoleOutputCharacter( hConsoleOut, &BlankCell, 1, Old, &Dummy );

/* Draw new face, then clear screen lock */
WriteConsoleOutputCharacter( hConsoleOut, &MyCell, 1, Coords, &Dummy );
WriteConsoleOutputAttribute( hConsoleOut, &MyAttrib, 1, Coords, &Dummy );
ReleaseMutex( hScreenMutex );

/* Increment the coordinates for next placement of the block. */
Old.X = Coords.X;
Old.Y = Coords.Y;
Coords.X += Delta.X;
Coords.Y += Delta.Y;

/* If we are about to go off the screen, reverse direction */
if( Coords.X < 0 || Coords.X >= csbiInfo.dwSize.X )
{
Delta.X = -Delta.X;
Beep( 400, 50 );
}
if( Coords.Y < 0 || Coords.Y > csbiInfo.dwSize.Y )
{
Delta.Y = -Delta.Y;
Beep( 600, 50 );
}
}
/* Repeat while RunMutex is still taken. */
while ( WaitForSingleObject( hRunMutex, 75L ) == WAIT_TIMEOUT );

// not sure if we need this??
ExitThread(0);

return 0;
}

void WriteTitle( int ThreadNum )
{
char NThreadMsg[80];

sprintf( NThreadMsg, &quot;Threads running: %02d. Press 'A' to start a thread,'Q' to quit.&quot;, ThreadNum );
SetConsoleTitle( NThreadMsg );
}

void ClearScreen( void )
{
DWORD dummy;
COORD Home = { 0, 0 };
FillConsoleOutputCharacter( hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, Home, &dummy );
}
 
I don't know if this will be good news or bad news, but after finding nothing wrong with a quick desk check, I ran your program on my machine and it ran fine. After pressing &quot;a&quot; a bunch of times I had a zoo of multi-colored happy faces bouncing around in the console. I pressed &quot;q&quot; and all the threads exited cleanly.

This has nothing to do with the problem, but the answer to the question in the code about &quot;do I need ExitThread&quot; is, &quot;no&quot;. You don't need exit thread. It is fine just to exit from the thread by returning zero. (Your program works either way.)
 
use debugger to see what is exactly inside.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
if your OS is win me/95/98 CreateThread function must not have the last argument as NULL.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top