educative.io

Code not running on terminal

ANY OF THE SAMPLE CODE IS NOT RUNNING ON THE TERMINALS. KINDLY FIX.


Course: https://www.educative.io/courses/concurrency-with-modern-cpp
Lesson: Educative: Interactive Courses for Software Developers

Hi @Aditya_Sharma,
The code appears to be functioning correctly on our end. Please double-check your internet connection.

If you continue to experience difficulties, a screenshot of the specific error or undesired output would be helpful for a more accurate diagnosis and resolution.


It says “Something went wrong, please try again.”

@Aditya_Sharma, thank you for sharing the screenshot. Can you please share the lesson link as well?

I’m working on concurrency with modern C++

Also I am having a problem I am working on. I have a function that runs in a thread in while loop. In that function I want to do something after every x milliseconds. But I don’t want a sleep since I still want then while loop to keep executing. I am not able to see a possible solution to this. Below is a rough structure of my problem. Do you have any suggestions. Sorry I am in a situation right now and don’t have patience to do search on forums.

void myfunc(){
while(true){
some code
{
do this part after every x milliseconds. without a sleep in my function
}
some code
}
}

I would think this should be something like a background timer which becomes true after every x milliseconds

@Aditya_Sharma

  1. The codes are working fine at our end. You need to recheck your internet connection. Attaching a screenshot of the correct output below:

If you encounter the error after making modifications to the code, kindly provide the updated code. This will assist me in identifying the issue and offering specific debugging assistance tailored to your changes.

  1. Regarding your second concern, you can achieve it by using a combination of std::chrono and a condition variable. Here’s a rough outline of how you can modify your myfunc:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool timerExpired = false;

void timerFunction(int interval) {
    while (true) {
        std::this_thread::sleep_for(std::chrono::milliseconds(interval));
        {
            std::unique_lock<std::mutex> lock(mtx);
            timerExpired = true;
        }
        cv.notify_one();
    }
}

void myfunc() {
    while (true) {
        // some code

        {
            std::unique_lock<std::mutex> lock(mtx);
            cv.wait(lock, [] { return timerExpired; });
            timerExpired = false;
        }

        // do something after every x milliseconds without using sleep

        // some code
    }
}

int main() {
    int interval = 1000; // set your desired interval in milliseconds

    std::thread timerThread(timerFunction, interval);
    std::thread workerThread(myfunc);

    timerThread.join();
    workerThread.join();

    return 0;
}

In this example, timerFunction runs in a separate thread and notifies myfunc using a condition variable (cv) when the specified time interval has passed. myfunc waits for the notification and then proceeds to execute the part of the code that you want to run after every x milliseconds.

Make sure to adjust the interval variable according to your desired time interval. This way, you avoid using sleep in your main loop while achieving the desired periodic behavior.

Another way to achieve this is by using std::chrono and checking the elapsed time within the loop. You can compare the current time with the last time you performed the operation and execute it only if the elapsed time is greater than or equal to x milliseconds. Here’s a simplified example:

#include <iostream>
#include <chrono>
#include <thread>

void myfunc() {
    using namespace std::chrono;

    auto lastTime = steady_clock::now();
    const auto interval = milliseconds(1000);  // Change this to your desired interval

    while (true) {
        // Some code before the timed operation

        auto currentTime = steady_clock::now();
        auto elapsedTime = duration_cast<milliseconds>(currentTime - lastTime);

        if (elapsedTime >= interval) {
            // Do this part after every x milliseconds
            std::cout << "Doing timed operation\n";
            
            // Update lastTime to the current time
            lastTime = currentTime;
        }

        // Some code after the timed operation
    }
}

int main() {
    std::thread myThread(myfunc);

    // Do other work in the main thread

    myThread.join();

    return 0;
}

This way, the loop will keep executing continuously, but the specific operation inside the loop will only be performed if the elapsed time is greater than or equal to your specified interval (x milliseconds in this case).

Thank you. I have a question regarding your first approach.
Will the cv.wait(lock,[]{return timerExpired;}); is blocking call? since it will wait for the timerExpired to return true. In that case it will work as a sleep.

Yes, you are correct. The cv.wait(lock, []{return timerExpired;}); line is a blocking call. It essentially puts the thread to sleep until the condition timerExpired becomes true, at which point it wakes up and continues execution.

In this case, it serves a similar purpose to a sleep, but with the advantage that it can be interrupted by the condition variable being notified. When the timer expires, the timerFunction sets timerExpired to true, and then notifies the condition variable, causing any waiting threads (such as the one inside myfunc) to wake up and continue processing.

So while it may seem similar to a sleep, it allows for more responsive behavior by letting the thread wake up immediately when the timer expires rather than having to wait for the entire sleep duration.

Thank you both the examples were helpful. :+1:

Hi Komal_Arif
I have the same error for any page “Something went wrong, please try again.” , even using incognito.
and I don’t believe it is internet, as my internet is normal.

Link First Ruby Program

Hi @Thiago_Silva_Farias, it seems like there might be a weak connection issue on your end. Could you please try executing the code again after ensuring that your internet connection is stable? I’ve just tested all the code widgets on my end, and they are functioning properly for me. Let me know if you encounter any further issues or if there’s anything else I can assist you with!