Timer Solution

From MWCSWiki

Jump to: navigation, search

Timer Solution

I used two threads but the timer did not interrupt the cin statement. This interrupts the cin statement

pthread_t thr1; //thread for threading

int create1, create2; //used for return value from thread creation

while ( !scrambleGame.gameEnded() )

{

scrambleGame.startGame();

if ( !scrambleGame.gameEnded() )

{

create1 = pthread_create( &thr1, NULL, input, NULL);

create2 = pthread_create( &thr1, NULL, timer, NULL);

pthread_join( thr1, NULL );                                 

}

}

void *input( void *ptr )
{
        finished = false;
        scrambleGame.promptUserInput();
        finished = true;
}

void *timer( void *ptr )
{
        time_t startTime, currentTime; //times used to measure elapsed time
        startTime = time ( NULL );
        currentTime = time ( NULL );
        while ( currentTime - startTime < 60 && !finished )
        {
                currentTime = time( NULL );
        }
        if ( !finished )
                scrambleGame.timeOut();

        finished = false;
}