Mario Camia
Mario Camia

Reputation: 113

How can I print two functions at the same time in C?

Im doing a game in C on console application and I need to print a timer while the user play the game at the same time, I don't know how to program that well, actually I just start using this compiler so I don't know a lot of multithreads and stuff but this is the far as I get, here is an example of two functions the first one is the "timer" and the second its just some question, my doubt is if I can run these 2 functions at the same time, and how I can print the timer while I can answer those 2 question without interrupting the console.

EXAMPLE:

#include <windows.h>
#include <stdio.h>
#include <time.h>


DWORD WINAPI Timer(int limit)
{
    int secs = 1;
    time_t unix;

    struct tm * timeinfo;
    time(&unix);
    timeinfo = localtime(&unix);

    int t1 = timeinfo->tm_sec;
    int t2 = timeinfo->tm_sec;
    int i = 0;

    while(1 == 1)
    {
       time(&unix);
       timeinfo = localtime(&unix);
       if((t1 + i)  == timeinfo->tm_sec)
       {
              system("cls");
              printf("time left %d seconds\n", timeinfo->tm_sec - t2 - limit);
              i++;
       }
       if(timeinfo->tm_sec >= (t1 + limit))
       {
            break;
            printf("Your time its done");
       }
    }
    return 0;
}

DWORD WINAPI Questionary()
{
    puts("testing\n\n");

    int age, height;
    printf("Please write your age: ");
    scanf("%d", &age);
    printf("Please write your height: ");
    scanf("%d", &height);
    printf("\n\nThe numbers written are %d y %d", age, height);
    return NULL;
}
int main()
{
    int i, limit;
    HANDLE tempo;
    HANDLE questions;
    DWORD ThreadId;

    printf("\nHow much time would you like for your timer countdown? ");
    scanf("%d", &limit);

    //Funcion Handle
    questions= CreateThread(NULL,0,Questionary,1,0,&ThreadId);
    WaitForSingleObject(preguntas,INFINITE);

    tempo= CreateThread(NULL,0,Timer(limit),1,0,&ThreadId);
    WaitForSingleObject(tiempofinal,limit*40);


    return 0;
}

Upvotes: 1

Views: 276

Answers (3)

oldTimes
oldTimes

Reputation: 259

As i know there is no way to write to console from different threads at the same time. Altough it looks two thread are writing at the same time, actually one of them that write at a time (Which catch the console to write). In this way altough two thread is running at the same time, it does not make sense.

I think you can solve your problem as a little tricky in one thread but it is not not good. But i think you can gui components and from two different threads, you can set field of this component and this satisfy your requirement.

Upvotes: 0

RedComet
RedComet

Reputation: 1202

What you want with threads, can't be accomplished with the line oriented functions, something like ncurses would be necessary for multithreaded console read/write operation.

What Joachim Pileborg, wrote is your best chance, the limitation is that your clock can't be ticking while you wait for input., if you poll every 1 second, writing the time and then waiting for the answer, most humans would not be able to reply on 1 second, so every 10 seconds would perhaps be the most sensible.

An alternate method to present the time ticking every second while still polling every second would be to save the input of that second on a accumulative buffer, and rewrite it on the screen on next input polling, besides the time ticking. This does have the danger of missing some input done while the loops alternate and is more complicated to code.

This is a case where a Graphical interface would be better suited for your needs.

Y seria buena idea que traduzcas al inglés tus nombres de variables para ayudar a una más rápida comprensión del contenido de tu programa, ya que es lo que entiende la mayoría por acá.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

You can print the question, and instead of waiting for an answer in a blocking call (like scanf) you loop while polling for new input, and use ANSI escape codes to print the time at another position.

Something like the following pseudo-code:

print(question)
while (current_time < timer_end)
{
    if (have_input())
        break

    save_current_cursor_position()
    move_cursor_position(x, y)
    print(timer)
    restore_saved_cursor_position()

    very_short_sleep()
}

Upvotes: 3

Related Questions