Command injection

I want to issue a command while another command is being executed in C++. For example wait command takes time for execution, and during this time I want to inject a different command and the wait command is completed.

Thanks

well, there are at least 2 basic ways to do this.
1) instead of waiting, keep track of elapsed time and go run the other code while you wait on the time to reach the next mark to go back to whatever was 'waiting'. This is a type of scheduling, and suited for single threaded logic.
2) multi-threading. One thread can happily sleep while another works. With multi-core cpus, you can also do up to # of cores things at one time with threads.

it would be unwise to try to manually break a sleep command; I think that has OS level implications. A busy waiter loop can be broken of course, but that is just going to end up being some weird way to do #1 above.
Last edited on
I am getting the following error
class Timer
{

private:
std::chrono::time_point<std::chrono::steady_clock> bStartTime;
std::chrono::time_point<std::chrono::steady_clock> bEndTime;
bool bRunning = false;

public:
void start();
void stop();
double elapsedMilliseconds();
double elapsedSeconds();
};

void Timer::start()
{
bStartTime = std::chrono::steady_clock::now();
bRunning = true;
}
void Timer::stop()
{
bEndTime = std::chrono::steady_clock::now();
bRunning = false;
}

double Timer::elapsedMilliseconds()
{
std::chrono::time_point<std::chrono::steady_clock> endTime;
if (bRunning)
{
endTime = std::chrono::steady_clock::now();
}
else
{
endTime = bEndTime;
}

return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - bStartTime).count();
}

double Timer::elapsedSeconds()
{
return elapsedMilliseconds() * 1000;
}
warning C4244: 'return': conversion from '_Rep' to 'double', possible loss of data
with
[
_Rep=__int64
]
I have used int and float as well in place of double.
When I use auto, error a function that returns 'auto' cannot be used before it is defined is generated.Any possible solutions. Thanks
too much code, not sure what you broke. all you need is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct hrtimer
{
 high_resolution_clock::time_point s; 
 duration<double> time_span;
 void start(){s = high_resolution_clock::now();}
 double stop()
 {    
   time_span = duration_cast<duration<double>>( high_resolution_clock::now() - s);
   return time_span.count();
 }
};

int main()
  {
	hrtimer h;
	h.start();
        sleep(5);  //unix (unistd) one, 5 sec
	cout << h.stop() << endl; 
}

C:\c>a
5.00018  //pretty close. you can't time it exact because the timer takes time...
Last edited on
Consider the above code I have posted. I

Timer timer;
timer.start();


while((ExecuteTMCCODE("\\..........")))

{

if (timer.elapsedSeconds() > 90.0 && timer.elapsedSeconds() < 95.0)

{
ExecuteTMCRKPTRCommand("-----");
break;
}
}

//Stop Timer
timer.stop();
I want to insert this command ExecuteTMCRKPTRCommand("-----");in between when block of commands ExecuteTMCCODE("\\..........") are running. But it is not possible to stop the execution of ExecuteTMCCODE("\\..........") untill it is completed. So it is not possible to insert the command. Also I used timer outside the ExecuteTMCCODE("\\..........") without success. Any useful suggestions, please
Last edited on
idk. Should execute simultaneously. can't really make any guarantees tho how well it will actually work. Depending on how heavy the "command" is might need to create a new process to run it. *untested but i've used similar code in the past.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void thread(bool& sync) {

//ExecuteTMCRKPTRCommand("-----");
	sync = false;
		
}
//Timer timer;
//timer.start();

int main() {
bool sync = true;
	while (sync) {

		//ExecuteTMCCODE("\\..........")

			if (1) {
				CreateThread(0, 0, LPTHREAD_START_ROUTINE(thread), &sync, 0, 0);
			}
	}
}
Any suggestions on using threads in solving above problem. Thanks
Topic archived. No new replies allowed.