Note that there are some explanatory texts on larger screens.

plurals
  1. POset flag in signal handler
    primarykey
    data
    text
    <p>In C++11, what is the safest (and perferrably most efficient) way to execute unsafe code on a signal being caught, given a type of request-loop (as part of a web request loop)? For example, on catching a SIGUSR1 from a linux command line: <code>kill -30 &lt;process pid&gt;</code></p> <p>It is acceptable for the 'unsafe code' to be run on the next request being fired, and no information is lost if the signal is fired multiple times before the unsafe code is run.</p> <p>For example, my current code is:</p> <pre><code>static bool globalFlag = false; void signalHandler(int sig_num, siginfo_t * info, void * context) { globalFlag = true; } void doUnsafeThings() { // thigns like std::vector push_back, new char[1024], set global vars, etc. } void doRegularThings() { // read filesystem, read global variables, etc. } void main(void) { // set up signal handler (for SIGUSR1) ... struct sigaction sigact; sigact.sa_sigaction = onSyncSignal; sigact.sa_flags = SA_RESTART | SA_SIGINFO; sigaction(SIGUSR1, &amp;sigact, (struct sigaction *)NULL); // main loop ... while(acceptMoreRequests()) { // blocks until new request received if (globalFlag) { globalFlag = false; doUnsafeThings(); } doRegularThings(); } } </code></pre> <p>where I know there could be problems in the main loop testing+setting the globalFlag boolean.</p> <p>Edit: The <code>if (globalFlag)</code> test will be run in a fairly tight loop, and an 'occasional' false negative is acceptable. However, I suspect there's no optimisation over Basile Starynkevitch's solution anyway?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload