C language > Expert questions
How to interrupt thrd_sleep?
Werner:
The C standard reads in
7.26.5.7 The thrd_sleep function (p. 2):
"The thrd_sleep function suspends execution of the calling thread until ... or a signal which is not being ignored is received. If interrupted by a signal ..."
How can I achieve this?
John Z:
Hi,
I think you need to be a bit more specific, or detailed, in your question.
The function is in <threads.h> and of the form
int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
The help file in Pelles C has information.
John Z
John Z:
Hi Werner,
If you were asking how to terminate a sleep before it ends you'll want to look at signal.
The C library function void (*signal(int sig, void (*func)(int)))(int) sets a function to handle signal i.e. a signal handler with signal number sig.
Here is some example code: https://www.tutorialspoint.com/c_standard_library/c_function_signal.htm
If this is not what you were looking for please explain more clearly.
Regards,
John Z
Werner:
I apologize! I thought it were obvious that I intend to terminate the sleep.
Thanks for trying to help me, but I still don't get it.
Before asking, I unsuccessfully tried the attached code which does NOT terminate the sleep. Where am I wrong?
Tia.
Werner
John Z:
Hi Werner,
I copied the example code at the link I posted and just change sleep(1) to _sleep(1).
Complied as a console and ran it. It worked quite well. When I hit CTRL+C it sends a 'signal' SIGINT to the sleep procedure and it terminates.
The key to interrupting sleep is to establish a response to any signal using signal(SIGINT, sighandler); The first parameter can be any valid signal that you want to raise. The link show the signals that can be responded to.
I tested your code sample and it works if you replace thrd_sleep with _sleep(SECS)
I'll take a further look at your code to see if anything is obvious with thrd_sleep (I've not used that before) , but
since it terminated immediately I'd look at how the timer is being set first.
Meanwhile you might try the link I posted to see if that works for you.
John Z
Navigation
[0] Message Index
[#] Next page
Go to full version