Help with this timer please

Jan 3, 2019 at 10:20pm
I would like help with this timer using <chrono> instead of <ctime> to get the current time and duration so it doesn't pause my whole program when I run it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
case ROOM_TALK:
	  {
		  u_char state = u_char(f[0x01]);
		  string nickname = string(f[0x02]);
		  int mtimer_size = SizeOfArray(mtimer_array);

		  boost::regex re(nickname, boost::regex::icase);
		  boost::cmatch matches;

		  for (i = 0; i < mtimer_size; i++) {
			  if (state != 0 && boost::regex_match(mtimer_array[i][0].c_str(), matches, re)) {

				  string str1 = mtimer_array[i][1];
				  int myint1 = stoi(str1);

				  clock_t startTime = clock(); //Start timer
				  double secondsPassed;
				  double secondsToDelay = myint1;
				  bool flag = true;
				  while (flag)
				  {
					  secondsPassed = (clock() - startTime) / CLOCKS_PER_SEC;
					  if (secondsPassed >= secondsToDelay)
					  {
						  //do  something like blocking their microphone after specified time)
						  flag = false;
					  }
				  }
			  }
		  }
	  } break;		 
Last edited on Jan 4, 2019 at 5:09am
Jan 4, 2019 at 12:21am
This code looks like it will be in a very tight loop, except you bounce out of the loop after one pass by setting flag to false. I'm not quite sure what you're trying to do here.
Jan 4, 2019 at 5:14am
Well i edited the code so you can (hopefully) get a better idea.

I was doing this before and it's doing the same thing with WAY less lines of code.

1
2
3
4
                                                string str1 = mtimer_array[i][1];
						int myint1 = stoi(str1);
						wait(myint1);
						// do something 
Jan 4, 2019 at 5:16am
I normally don't ask for help I can usually figure this stuff out on my own perhaps over time I will come back to it and figure it out.

the frustrating part is putting it in terms of "what im trying to do" for users without giving away my whole project
Last edited on Jan 4, 2019 at 5:17am
Jan 4, 2019 at 6:19am
> the frustrating part is putting it in terms of "what im trying to do"
Sleep for time span
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
¿close enough?
Jan 4, 2019 at 6:40am
Thanks ne555 i will give this a shot appreciate that reference!
Jan 4, 2019 at 6:51am
I tried this one before ne555 it still pauses the whole process

1
2
3
4
5
6
7
8
9
10
for (i = 0; i < mtimer_size; i++) {
			  if (state != 0 && boost::regex_match(mtimer_array[i][0].c_str(), matches, re)) {

				  string str1 = mtimer_array[i][1];
				  int myint1 = stoi(str1);
				  std::this_thread::sleep_for(std::chrono::seconds(myint1));
				  //blocking mic
                                  //unblocking mic
			  }
		  }
Jan 4, 2019 at 7:54am
I tried this one before ne555 it still pauses the whole process
It pauses the actual thread. If you want to do something during the sleep you need to run a parallel thread.
Topic archived. No new replies allowed.