Thread: Multithreadding in C?

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Multithreadding in C?

    In my project, I'm supposed to print three lines of falling objects that are independent of each other and have random positions. My code goes to the second line, but the first line doesn't stay intact. It disappears and the object at the second line replaces it.

    Code:
    #include <stdio.h>
    #include <mp.h>
    
    int main()
    {
        //define the variables needed
        int lives = 3;
        int x = pickRandom(0,79);
        int y = 1;
        int letter = pickRandom(1, 3);
        int ctr;
        int x2 = pickRandom(0, 79);
        int y2 = 1;
        int letter2 = pickRandom(1, 3);
        
    //sets background color    
    setBackground(BLUE);
    clrscr();
    while(lives != 0) //sets the continuous loop while lives is not yet 0
    {    //redraws trash everytime      
                   if( y < 25)   
                   {
                    gotoxy(x, y);
                    printf("N");
                    delay(500);
                    printf("\b ");
                    gotoxy(x, y++);                
                    ctr = 1;
                    }
                    else if (y > 24)
                    {
                    letter = pickRandom(1, 3);
                    x = pickRandom(0,79);
                    y = 1;
                    }
                     while(ctr == 1 && y == 3)
                    {
                    gotoxy(x2, y2);
                    printf("B");
                    delay(500);
                    printf("\b ");
                    gotoxy(x2, y2++);
                    }      
    
    
    }
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    My code goes to the second line, but the first line doesn't stay intact.
    Which line are you talking about?

    Just so you know, if it manages to get in to the while loop (which is a rare in the first place) it will never get out since none of the conditions get changed.

    Also, if the code hits the "else if" statement it will never hit the while loop.

    Lastly, in what way is this multithreaded, or game related?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    I meant printing out an object in the first line.

    I know that. What i meant to say was, I need to print 3 different lines at different conditions, and what my code does is it stops what it is printing in the first line and goes to the second line, and so on. I need to see all 3 outputs at the same time.

    I am assuming that this is called multithreading because i think it is difficult to print three different lines at the same time. I need this code for my game because it is the code for falling objects.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    A thread is when something is being processed in a completely different part of the application at the same time as the main part of the application. It's like having two int main() loops running, but a thread will only last as long as a function call would, in a sense. There you have one while loop printing 2 lines (I don't see 3 printf's that actually print anything besides "N" and "B") inside a main. To use threading you would need to use CreateThread() or _beginthread() or something of the like.

    As for printing 2 or 3 lines at a time, I don't quite get why or what you're trying to accomplish here. I really hope you're updating 'lives' inside the gotoxy() function (which would mean its either global or defined else how/where) otherwise that loop will run forever.

  5. #5
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    It looks like you are trying to do a Matrix style thing where you have random characters falling down the screen. You do not need multi-threading to accomplish this, and in fact, multi-threading would add a nightmarish amount of complexity for this simple task.

    I think the problem is that you are thinking about this the wrong way. It looks to me like you mean for your code to draw one line all the way through, and then draw the next line all the way through, and then the third line all the way through. Thus your logical desire to multi-thread so that all three happen at the same time instead of one after the other.

    As a quick style note, your indentation is inconsistent. The section from //sets background color down looks like a new function at a glance, and the indentation inside the while(lives != 0) loop is all almost aligned with each other, removing all benefits of indentation. Fixing that might make it easier for you to spot things in the code you've written, whether errors or opportunities to change things.

    Instead of thinking in terms of each of the lines of characters being processed independantly, think instead of your loop as updating each line in turn until you are done. Here is pseudo-code to explain what I mean:

    Code:
    determine random starting points for each line (x1,y1 , x2,y2 and x3,y3)
    draw a random character at each of these starting points
    
    perform the following loop while y1, y2 and y3 are still on the screen
        delay for half a second
    
        if y1 is above the bottom of the screen
            erase the character at x1,y1
            add 1 to y1
            if y1 is still above the bottom of the screen
                then draw a new random character at x1,y1
    
        if y2 is above the bottom of the screen
            erase the character at x2,y2
            add 1 to y2
            if y2 is still above the bottom of the screen
                then draw a new random character at x2,y2
    
        if y3 is above the bottom of the screen
            erase the character at x3,y3
            add 1 to y3
            if y3 is still above the bottom of the screen
                then draw a new random character at x3,y3
    This will draw the top character of each line, wait half a second, and then appear to move each one down (changing it to a new random character) until all three have moved all the way down and "fall off" the screen.

    Instead of using x1,y1, x2,y2 and x3,y3 you could use an array of structs, which would allow you to easily add additional lines, but I would suggest getting this working first to help you wrap your head around the idea.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Thanks for the info. It worked.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't see any multi-threading in this entire thread.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    he just posted an algorithm that would simulate multithreading

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That does not simulate multi-threading.

Popular pages Recent additions subscribe to a feed