Program is behaving weirdly: new changes are no longer reflected

Problem description:
So I have been noticing a weird behavior: could be related to std::this_thread::sleep_for(delay); but I have the following function and every time I run it, it doesn’t seem to take into account the delay no matter how much of I add. I also tried changing the print statement and that doesn’t get reflected either.

  void Add(const TimerInfo& timerInfo)
  {
      std::chrono::seconds delay(20);
      /std::this_thread::sleep_for(delay);

    TimePoint curTime = Clock::now();  
    
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(curTime - startTime);
    std::cout << "Time elapsed = " << duration.count() << "\n";

  }  

Expected behavior:
The delay to work or any changes made in the function

Actual behavior:
Described in the first part

Steps to reproduce:

// PQueue.hpp
#pragma once

#include "CallbackTimer.hpp"

#include <queue>
#include <thread>
#include <chrono>
#include <vector>
#include <iostream>

static const TimePoint startTime = Clock::now();

class PQueue
{
public:
  void Add(const TimerInfo& timerInfo)
  {
    // std::cout << "Hola\n";
      // std::chrono::seconds delay(20);
      // std::this_thread::sleep_for(delay);

    TimePoint curTime = Clock::now();  
    
    // auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(curTime - startTime);
    // std::cout << "Time-elapsed = " << duration.count() << "\n";

  }  

private:
  std::priority_queue<unsigned int, std::vector<unsigned int>, std::greater<unsigned int>> _pQueue;
};



// CallbackTimer.hpp

#pragma once

#include <iostream>
#include <queue>
#include <chrono>
#include <iostream>
#include <functional>
#include <array>



using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
using Callback = std::function<void()>;

struct TimerInfo
{
  unsigned int expiry;
  Callback callback;
  unsigned int refTime;
};

std::array<TimerInfo, 2> scheduleA = 
{
  TimerInfo{1000, []() { std::cout << "A-1\n"; }},
  TimerInfo{5000, []() { std::cout << "A-2\n"; }}
};

class CallbackTimer
{
public:
  void Add(TimerInfo timerInfo)
  {
   //  unsigned int curTime = 
   // _pQueue.push(timerInfo);   
  }

};

// main.cpp
#include <main.h>

#include <util.h>
#include <iostream>
#include "PQueue.hpp"

using namespace std;

int main(void) {
  PQueue pqueue;
  pqueue.Add(scheduleA[0]);
	return 0;
};

Here’s my repl:
https://replit.com/@huzzziiii/FaF#src/PQueue.hpp

Surprised how the help community isn’t active here

The issue you’re experiencing is related to the usage of std::this_thread::sleep_for(delay); within the Add function. The sleep function pauses the execution of the current thread for the specified duration, but it doesn’t affect the behavior of other code outside the sleeping thread. Make sure you’re calling the Add function with different inputs or parameters to observe varying behavior.

The print statement std::cout << "Time elapsed = " << duration.count() << "\n"; is relying on the startTime and curTime variables, but they don’t appear to be updated within the Add function.

I hope this helps!

Sorry, I didn’t see the topic from 2 days ago, I try my best to help as many people as possible.

Why do I have to test with different inputs tho? Add is already called. How about you try running my repl? I hope it’s visible? Let me know if it isn’t

For some reason I am now running into sh: line 1: ./main: cannot execute binary file: Exec format error exit status 126 which is unrelated to my program.

Anyways, my logic adds a delay, determines the current time and prints it. I don’t see what could be an issue here

Hello? It’s been a while since there’s been no follow up

Hey @huzzziiii!

I apologize for the delay on this. We will look into this and will follow up as soon as we have an update.

It looks like your files are within a src/ folder instead of being in the top-level of your Repl. If you move the code files outside of the src/ folder, that should work!

3 Likes