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);