Sleep() for a specific function?

So I have a loop that is running and (keeping a connection to the server).
I want to send messages to that server in a (4) seconds interval wihout losing the connection.


How can I have Sleep()for that specific Function without stopping other loops?

1
2
3
4
5
6
7
8
9
10
11
12
13
  void CMI::SendLoop()
{
    
    while (Sending)
{
    
    std::this_thread::sleep_for(seconds(4)); //connection is lost
    SendMessage("Im here");
    

       }

}


Last edited on
if this is in it's own thread, you can use sleep_for(): https://www.cplusplus.com/reference/thread/this_thread/sleep_for/ but when you wake up, be sure to check whether the process is trying to exit.

If it's an event driven system like windows then I believe there's a way to schedule an event for the future. Then you're keep-alive could just process the event and schedule another.
I think either new thread or C++ 20 coroutine.

Following sample compileable code creates a new thread that does the networking stuff while the main thread does the other work:

See comments for explanation:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <future>
#include <thread>
#include <iostream>
#include <chrono>
#include <utility>

void NetworkLoop(std::future<bool> condition)
{
	// Messeage sending procedure
	auto SendMessage = [](const char* message)
	{
		std::cout << message << std::endl;
	};

	// main thread will signal end of program to this thread trough this variable
	auto status = std::future_status::deferred;

	// Keep sending packets until program exit every 2 seconds
	while (status != std::future_status::ready)
	{
		SendMessage("Im here!");
		status = condition.wait_for(std::chrono::seconds(2));
	}
}

int main()
{
	// Create communication channel between this and network thread
	std::promise<bool> promise;
	std::future<bool> condition = promise.get_future();

	// Create network thread
	std::thread network(NetworkLoop, std::move(condition));

	// Main program runs for 10 seconds
        // it does nothing special by default but it could so here...
	std::this_thread::sleep_for(std::chrono::seconds(10));

	// Tell network thread to stop
	promise.set_value(true);

	// Join thread
	network.join();

	std::cout << "Program is finished!" << std::endl;
}
Last edited on
another way is, if you already have like a main loop in your main program code, just put a timer there, and if enough time has passed, do the activity.
that changes the loop to
get current time

loop
{
things;
if current time - last time > time to wait
{
send keep alive message
update last time
}
more things;
}

single threaded programs are starting to be a thing of the past, but if you were on an embedded system with 1 cpu or something, or just starting out and don't want to tackle threading yet, etc this will do it.
Last edited on
single threaded programs are starting to be a thing of the past

True but, programs such as calculators, screenshot takers or text editors are likely to remain single threaded for ever regardless of hardware :)

I'm more afraid of what will happen when quantum computers become a thing, will everything go out of scope by design?
text eds are often threaded now, mixed bag on those.

quantum computers are so far past the end of my life I can't worry about it :) But many things will go out of scope. A lot of security goes out the window, in theory.
as I understand the theory (which is not well) they can do anything the old fashioned way, its just not ideal for the hardware.
Last edited on
Topic archived. No new replies allowed.