Terminal Loading bar not updating correctly

I’m building a Multilevel Feedback Queue Scheduling(MLFQ) sorting Algorithm simulation and I’m having the results of each process print out to the terminal and they each have a loading bar and I need them to update accordingly. Right now I can get the loading bar to update correctly
This is what it looks like when it is printing:

Timestamp Process ID
=============== ] ======== ] [8] Process 9 [ [5] Process 6 [ 4 [

It is not having each process on its own separate line and its not updating correctly. Is the problem that I’m using threads? Any help would be appreciated

//.h file
 #pragma once
 #ifndef MLFQ_H
 #define MLFQ_H

#include <vector>

struct Process {
    int processID;
    int arrivalTime;
    int burstTime;
    int priority;

    Process(int id, int arrival, int burst, int prio) :
        processID(id), arrivalTime(arrival), burstTime(burst), priority(prio) {}
};

class MLFQ {
private:
    std::vector<Process> processes;
    std::vector<Process> queue0; // Highest priority
    std::vector<Process> queue1; // Medium priority
    std::vector<Process> queue2; // Lowest priority

 public:
    MLFQ(std::vector<Process>& procs);
    void runMLFQ();
 };


#endif
//.cpp file
#include "MLFQ.h"
#include <iostream>
#include <algorithm>
#include <thread>
#include <mutex>
#include <chrono>
#include <iomanip>

// Mutex for ensuring thread-safe access to cout
std::mutex coutMutex;

// Named constants
const int MAX_PROCESS_ID_WIDTH = 10;
const int MAX_TIMESTAMP_LENGTH = 10;
const int LOADING_BAR_WIDTH = 20;

// Helper function to print column headers
void printColumnHeaders() {
    std::lock_guard<std::mutex> lock(coutMutex);
    std::cout << "33[1;34m" << std::right << std::setw(MAX_TIMESTAMP_LENGTH) << "Timestamp "
        << std::left << std::setw(MAX_PROCESS_ID_WIDTH) << "Process ID" << std::endl;
}

// Helper function to print process message
void printProcessMessage(const Process& process, int currentTime) {
    std::lock_guard<std::mutex> lock(coutMutex);
    std::cout << "33[1;33m" << std::right << std::setw(MAX_TIMESTAMP_LENGTH) << "[" << currentTime << "] "
        << std::right << std::setw(MAX_PROCESS_ID_WIDTH) << "Process " << std::setw(2) << process.processID << " [";
}

// Helper function to print loading bar
void printLoadingBar(int progress) {
    std::lock_guard<std::mutex> lock(coutMutex);
    for (int i = 0; i < LOADING_BAR_WIDTH; ++i) {
        if (i < progress) {
            std::cout << "=";
        }
        else {
            std::cout << " ";
        }
    }
    std::cout << "] ";
    std::cout << std::flush;
}

// Helper function to execute process
void executeProcess(const Process& process, int& currentTime) {
    // Print process ID
    printProcessMessage(process, currentTime);

    // Simulate processing time
    for (int i = 0; i < process.burstTime; ++i) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        ++currentTime;
        // Print execution progress
        int progress = LOADING_BAR_WIDTH * (i + 1) / process.burstTime;
        std::cout << "r"; // Move cursor to the beginning of the line
        printLoadingBar(progress);
        std::cout << std::flush; // Flush the output buffer
    }

    std::cout << "33[K33[0m" << std::endl; // Clear the line and reset color
}


MLFQ::MLFQ(std::vector<Process>& procs) : processes(procs) {}

void MLFQ::runMLFQ() {
    // Print column headers
    printColumnHeaders();

    // Sorting processes by arrival time
    std::sort(processes.begin(), processes.end(), [](const Process& p1, const Process& p2) {
        return p1.arrivalTime < p2.arrivalTime;
        });

    int currentTime = processes.empty() ? 0 : processes[0].arrivalTime;

    // Create a thread for each process
    std::vector<std::thread> threads;
    for (const auto& process : processes) {
        threads.emplace_back([&]() {
            try {
                // Wait until the arrival time
                std::this_thread::sleep_for(std::chrono::seconds(process.arrivalTime - currentTime));

                // Update current time
                currentTime = process.arrivalTime;

                // Execute process
                executeProcess(process, currentTime);
            }
            catch (const std::exception& e) {
                // Print error message in red
                std::lock_guard<std::mutex> lock(coutMutex);
                std::cerr << "33[1;31mError: " << e.what() << "33[0m" << std::endl;
            }
            });
    }

    // Wait for all threads to finish
    for (auto& thread : threads) {
        thread.join();
    }
}

//main file
#include <iostream>
#include "MLFQ.h"


int main()
{
    std::vector<Process> processes = {
    Process(1, 0, 10, 1),
    Process(2, 1, 15, 2),
    Process(3, 2, 8, 0),
    Process(4, 3, 12, 1),
    Process(5, 4, 7, 2),
    Process(6, 5, 18, 0),
    Process(7, 6, 5, 1),
    Process(8, 7, 20, 2),
    Process(9, 8, 12, 0),
    Process(10, 9, 6, 1),
    Process(11, 10, 14, 2),
    Process(12, 11, 9, 0),
    Process(13, 12, 16, 1),
    Process(14, 13, 4, 2),
    Process(15, 14, 20, 0),
    Process(16, 15, 8, 1),
    Process(17, 16, 10, 2),
    Process(18, 17, 6, 0),
    Process(19, 18, 18, 1),
    Process(20, 19, 5, 2),
    Process(21, 20, 22, 0),
    Process(22, 21, 9, 1),
    Process(23, 22, 16, 2),
    Process(24, 23, 7, 0),
    Process(25, 24, 12, 1),
    Process(26, 25, 14, 2),
    Process(27, 26, 6, 0),
    Process(28, 27, 20, 1),
    Process(29, 28, 10, 2),
    Process(30, 29, 8, 0)
    };


    MLFQ mlfq(processes);
    mlfq.runMLFQ();

    return 0;
    
}


1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật