Thread: Multi tasking - Printing problem!

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    5

    Multi tasking - Printing problem!

    Hello everyone! I have lately started learning multi-tasking to use it in my project, but I have a small problem while printing messages. I have 3 tasks that I want to run at the same time.
    1-) Countdown
    2-) Printing time
    3-) Whatever function, printing a message and asking for a value.

    Here is my code:
    Code:
    #include <stdlib.h>#include <stdio.h>
    #include <windows.h>
    #include <ctype.h>
    #define TRUE 1
    
    int minutes = 0;
    int seconds = 0;
    int value;
    
    
    DWORD countdown() {
    
        while(TRUE) {
    
            printf("\r \t\t %2dm : %2ds",minutes,seconds);
            Sleep(1000);
            seconds++;
    
            if(seconds == 60) {
                minutes++;
                seconds = 0;
            }
        }
    }
    
    
    DWORD print_time() {
        while (TRUE) {
            printf("\r \t\t %2dm : %2ds", minutes, seconds);
        }
    }
    
    
    DWORD whatever() {
        printf("Please enter a value:");
        scanf("%d",&value);
    }
    
    
    int main() {
    
        HANDLE thread1,thread2,thread3;
        thread1 = CreateThread(NULL, 0, countdown, "", 0, NULL);
        thread2 = CreateThread(NULL, 0, print_time, "", 0, NULL);
        thread3 = CreateThread(NULL, 0, whatever, "", 0, NULL);
    
        WaitForSingleObject(thread1, INFINITE);
        WaitForSingleObject(thread2, INFINITE);
        WaitForSingleObject(thread3, INFINITE);
    
    }
    Expected results:
    0m:10s Please enter a value: 123
    All on the same line.

    However, the results that I get are:
    0m:10s3 Please enter a value:

    So as you can see, it writes the last character of the value after printing the time and not after the message. So the text is being overwritten. I think the problem is from \r but I am not sure though.

    I hope someone could help me. Thanks and happy new year!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    You have no synchronisation between threads, so absolutely anything can happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    5
    Ahh. And how do I fix that?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    I guess you could read the manual pages of functions related to the ones you've used, until you discover functions relating to synchronisation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 01:35 PM
  2. Multi-tasking.
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 11-12-2005, 04:43 PM
  3. Multi-tasking...
    By weraw in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-14-2001, 04:26 AM
  4. Multi-tasking In C
    By WERAW in forum C Programming
    Replies: 5
    Last Post: 12-11-2001, 05:41 AM
  5. Printing multi-lines?
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2001, 04:52 PM

Tags for this Thread