1

I've a library which accepts a vector of strings as argument & pushes it to a vector of vector of strings (2D vector). I've a condition that "right from the time 1st row is pushed to the 2D vector the timer should start & for every "X" th milisecond I want to append some special string "FLAG_BORDER" at the end of the vector of string that i receive as argument to library.

LIBRARY CODE

using namespace std;
vector<vector<sring>> vecOfVecStrs;
MyLibraryFunction(vector<string> vecstr)
{
    if(TimerLimitNOTHit()) // THIS FUNCTION TimerLimitNOTHit() checks if the "X" 
                           //    th milisecond is reached or not.
    {
          vecOfVecStrs.push_back(vecstr); // If "X"th ms time poeriod is NOT 
                                           // yet reached
    }
    else
    {
         vecstr.push_back("FLAG_BORDER");
         vecOfVecStrs.push_back(vecstr);
    }

APPLICATION CODE CALLING LIBRARY FUNCTION::

int main()
{
    do_something();
    vector<string> vecStr;
    while(vecStr = Get_VecTor_of_strings())
    {
        MyLibraryFunction(vecStr);
    }

How do I implement the function TimerLimitNOTHit() here which should keep track of the timer "X" milisec across function calls to "MyLibraryFunction()" in C++ ?

EDITED: SEEMS LIKE FOUND AN ANSWER. wILL IT WORK ?

int timeX = 30; //s
bool keepTrack = false;
int ci = 0;
std::vector<int> timeArray;
std::mutex m;

bool keepTracking()
{
    std::unique_lock<std::mutex> lock(m);
    return keepTrack;
}

void GlobalTimer()
{
    while (keepTracking())
    {
        std::this_thread::sleep_for(std::chrono::seconds(timeX));
        timeArray[ci] = 1;
    }
}



 std::thread t1(GlobalTimer);
codeLover
  • 3,720
  • 10
  • 65
  • 121

1 Answers1

1

Here is some inspiration, where the function TimerLimitNOTHit returns true if more than X ms has passed since it last returned true.

int X = ... // #0

bool TimerLimitNOTHit()
{
    static auto start = std::chrono::steady_clock::now(); // #1
    auto now = std::chrono::steady_clock::now(); 
    if (now - start > std::chrono::milliseconds(X))
    {
        start = now // #2
        return true;
    }
    return false
}

#0 Defining the distance in time between two calls to TimerLimitNOTHit that return true.

#1 Initialization depend on your application logic. In this example the function does not return true on the first call, this can changed by initializing start to zero.

#2 start value for the next iteration, again it depends on your application logic. If you want a more steady true you could do some modulo arithmetic.

Let me know if this is not what you were looking for.

Disclaimer

I don't really like the use of static variables, but without a "status" parameter to the function I don't see how it can be avoided. Furthermore, the use of the global X variable does not fall to my likening neither. The variable X could be changed to a template variable, this better show intent and makes it a compile time constant.

Jonas
  • 6,915
  • 8
  • 35
  • 53