Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Data display in terminal with using threads

I have a code that displays specific information on the display. When I run my code in debug mode - it works successful. But when I run it in release mode - it breaks down. I'd not wanna use most checks so I decided to use thread that waits for specific signal - a simple variable reference, then it changes text color, the text's displayed then it changes text color back to default. What do I do wrong?

void PrintStudent(Student& student, int active_field) {
string  str;
int sum = student.getName().size() +                             
                  student.getSurname().size() +
                  student.getMiddleName().size() + 2;

const int FIRST     = 50,
      SECOND    = 30,
      THIRD     = 20;

int field       = 0;

cout << "|";

thread _thread(print_active, &field, &active_field);

str.resize(FIRST - sum, ' ');

cout << student.getName() << " " << student.getSurname()
    << " " << student.getMiddleName() << str;

field = -1;
Sleep(1);
cout << "||";

sum = student.getDirection().size();
str.resize(SECOND - sum, ' ');

field = 1;
Sleep(1);
cout << student.getDirection() << str;

field = -1;
Sleep(1);
cout << "||";

sum = student.getGroup().size();
str.resize(SECOND - sum, ' ');

field = 2;
Sleep(1);
cout << student.getGroup() << str;

field = -1;
Sleep(1);
cout << "||";

sum = student.getNumberBook().size();
str.resize(THIRD - sum, ' ');

field = 3;
Sleep(1);
cout << student.getNumberBook() << str;

field = -1;
Sleep(1);
cout << "|" << endl;

_thread.join();

helper();
}

void print_active(int* field, int* active_field) {
  while (*field != *active_field) { }
  while (*field == *active_field)
    SetConsoleTextAttribute(hStdOut, activeItem);
  SetConsoleTextAttribute(hStdOut, inactiveItem);
}

Comments