Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:49:35+00:00 2026-06-14T22:49:35+00:00

Here is a sample of the main code (Library/stack.h doesn’t really matter, but in

  • 0

Here is a sample of the main code (“Library/stack.h” doesn’t really matter, but in any case, it is the last source included in this previous question of mine):

#include <stdlib.h>
#include <time.h>

#include <iostream>
#include <tinythread.h>

#include "Library/stack.h"

using namespace std;
using namespace tthread;

#define BOULDERspd 100

// ========================================================================= //

struct Coord {
    int x, y;
};

int randOneIn (float n) {
    return ((int) (n * (rand() / (RAND_MAX + 1.0))));
}
int randOneIn (int n) {
    return ((int) ((float) n * (rand() / (RAND_MAX + 1.0))));
}

// ========================================================================= //

#include <windows.h>
void gotoxy (int column, int line) {
    if ((column >= 0) && (line >= 0)) {
        COORD coord;
        coord.X = column;
        coord.Y = line;
        SetConsoleCursorPosition(
            GetStdHandle( STD_OUTPUT_HANDLE ),
            coord
        );
    }
}

void gotoxy (Coord pos) {
    gotoxy(pos.x, pos.y);
}

// ========================================================================= //

void render (char image, Coord pos) {
    gotoxy(pos);
    cout << image;
}

void unrender (Coord pos) {
    gotoxy(pos);
    cout << ' ';
}

// ========================================================================= //

char randimage (void) {
    return (rand() % 132) + 123;
}

mutex xylock;

class Boulder {
    char avatar;
    Coord pos;

    public:
        Boulder (int inix) {
            pos.x = inix;
            pos.y = 0;

            avatar = randimage();
        };

        void fall (void) {

            unrender(pos);
            pos.y++;
            render(avatar, pos);

            Sleep(BOULDERspd);
        };

        void live (void) {
            do {
                fall();
            } while (y() < 20);
            die();
        };

        void die (void) {
            unrender(pos);
            pos.y = 0;
        };

        int x (void) { return pos.x; };
        int y (void) { return pos.y; };
};

// ========================================================================= //

class thrStack: public Stack<thread*> {
    public:
        thrStack (): Stack<thread*> () { };

        void pushNrun (thread* elem) {
            push(elem);
            top->core->joinable();
        }
};

void randBoulder (void* arg) {
    srand(time(NULL));
    Boulder boulder(rand() % 40);

    boulder.live();
}

void Boulders (void* arg) {
    srand(time(NULL));
    thrStack stack;

    do {
        stack.pushNrun(new thread (randBoulder, 0));
        Sleep(rand() % 300);
    } while(1);
}

// ========================================================================= //
// ========================================================================= //

int main() {
    thread raining (Boulders, 0);

    raining.join();
}

I’m new to multi-threading so, to fiddle around with it, I’m trying to make a program that makes random characters constantly fall from the top of the screen, as if it were raining ASCII symbols.

I’ve noticed, however, a little (big) error in my coding:

bool xylock = false;

class Boulder {
    char avatar;
    Coord pos;

    public:
        Boulder (int inix) {
            pos.x = inix;
            pos.y = 0;

            avatar = randimage();
        };

        void fall (void) {

            unrender(pos);
            pos.y++;
            render(avatar, pos);

            Sleep(BOULDERspd);
        };

        void live (void) {
            do {
                fall();
            } while (y() < 20);
            die();
        };

        void die (void) {
            unrender(pos);
            pos.y = 0;
        };

        int x (void) { return pos.x; };
        int y (void) { return pos.y; };
};

Because the fall() function uses gotoxy, which changes the ‘global cursor’, multiple calls to gotoxy would mess up the intended execution of the program. If you try to compile the code as-is, you’d get falling letters that constantly switch position and leave garbage of themselves behind.

Is there any way to use or implement a lock for this and future situations alike with just TinyThread? What is the logic of locks implementing in C++, in general?


EDIT: Modified fall(); is it okay, Caribou?

        void fall (void) {
            lock_guard<mutex> guard(xylock);

            unrender(pos);
            pos.y++;
            render(avatar, pos);

            xylock.unlock();

            Sleep(BOULDERspd);
        };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:49:37+00:00Added an answer on June 14, 2026 at 10:49 pm

    You can use the tinythread lib:

    http://tinythreadpp.bitsnbites.eu/doc/

    Look specifically at lock_guard and mutex

    multiple calls to gotoxy would mess up the intended execution of the
    program. If you try to compile the code as-is, you’d get falling
    letters that constantly switch position and leave garbage of
    themselves behind.

    create a mutex object to synchronise on, and then in the function you want to be thread safe you create a local lock_guard using it. This mutex can be used in multiple places as well using the lock_guard.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's sample model classes, which being use with Entity Framework Code First: public class
here the sample case.. i want to display banner randomly by percentage based on
Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
Here is the sample code that I ran on Visual Studio 2010: #include <iostream>
Here is a sample code package org.example; import java.lang.reflect.Method; class TestRef { public void
I'm trying to connect to Informix database using sample code from https://www.ibm.com/developerworks/data/library/techarticle/dm-0510durity/ I'm using
I have here a simple image converter code using Magick++(ImageMagick interface) library.(Eclipse IDE) #include
What am I doing wrong here: class Helo { // main: generate some simple
Here is sample data <table class=sparql border=1> <tr> <th>abstract</th></tr> <tr> <td> Cologne is Germany&#39;s
Data: Here is sample table(TableA) data ID StartTime EndTime 1 2012-03-22 06:00:00.000 2012-03-22 06:30:00.000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.