I have a program which uses POSIX timer (timer_create()
). Essentially the program sets a timer and starts performing some lengthy (potentially infinite) computation. When the timer expires and a signal handler is called, the handler prints the best result yet that has been computed and quits the program.
I consider doing the computation in parallel using OpenMP, because it should speed it up.
In pthreads, there are special functions for example for setting signal masks for my threads or so. Does OpenMP provide such control, or do I have to accept the fact that the signal can be delivered to any of the threads OpenMP creates?
Also, in case I am currently in a parallel section of my code and my handler is called, can it still safely kill the application (exit(0);
) and do things like locking OpenMP locks?
OpenMP 3.1 standard says nothing about signals.
As I know, every popular OpenMP implementation on Linux/UNIX is based on pthreads, so OpenMP thread is pthread's thread. And generic rules of pthreads and signals apply.
No any specific control; but you can try to use pthread's control. Only problem is to know how much OpenMP threads are used and where to place controlling statement.
By default, yes, it will be delivered to any thread.
Usual rules about signal handler still applies. Functions allowed in signal handler are listed at http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html (at the end of page)
And
printf
is not allowed (write
is). You can use printf if you know that at the moment of signal printf is not used by any thread (e.g. you has no printf in parallel region).Yes it can:
abort()
and_exit()
are allowed from handler.Linux/Unix will terminate all threads when any thread does
exit
orabort
.You should not, but if you know that this lock will be not locked at the time of signal handler run, you can try to do this.
!! UPDATE
There is an example of adopting signalling to OpenMP http://www.cs.colostate.edu/~cs675/OpenMPvsThreads.pdf ("OpenMP versus Threading in C/C++"). In short: set a flag in handler and add checks of this flag in every thread at every Nth loop iteration.