1

Is a mutex or some kind of synchronization necessary in this situation

static int flag;
void sighandler(int sigid)
{
    if (sigid != SIGINT)
        return;
    flag = 1;
}

int main(void)
{
    if (signal(SIGINT, sighandler) == SIG_ERR)
        return -1; /* cannot handle it ? */
    while (flag == 0)
    {
        do_things();
    }
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I don't think so. It's the same thread. – Eugene Sh. Aug 28 '15 at 01:04
  • No. Your handler is invoked asynchronously to the calling thread - and all other threads. There should be no contention for the global variable. However, this is *only* true if "sighandler()" is the *only* place "flag" is ever modified. I'd also recommend an explicit 0 (I'm not somebody who believes "fewer characters is more readable"), and declaring it "volatile": `static volatile int flag = 0;`. – paulsm4 Aug 28 '15 at 01:23
  • Fewer characters is **less** readable but initializing to zero is not more readable it's superfluous. – Iharob Al Asimi Aug 28 '15 at 01:29
  • I strongly disagree: just because it's implicit, doesn't mean you shouldn't explicitly state it, too., I feel the same way about curly braces around a simple "if" block, for exactly the same reason. – paulsm4 Aug 28 '15 at 03:54
  • @paulsm4 I am very happy to hear that I am not the only one interested in the beauty and readability of code, we have to agree to disagree in what for us consitutes good style and that is valid. But we do agree in that style is **very important**. – Iharob Al Asimi Aug 28 '15 at 09:26
  • 1
    Side note: you almost certainly should be using `sigaction(2)` if you care about portability. The behavior of `signal(2)` varies across UNIX versions and is considered obsolescent by now. See the notes on portability on the manpage for `signal(2)`. – Filipe Gonçalves Aug 28 '15 at 09:28
  • @FilipeGonçalves I did read that before but didn't remember why `sigaction` was important. But anyway the question would still be valid if I use `sigaction`. Also, this is not the final implementation, I was actually doing that to let my program reach the cleanup routine to free all allocated memory just to pass `valgrind`'s test. I will however implement a robust and complete signal handler so knowing that is important. I was going to do some reaserch about it because I do remember that `sigaction` was better. – Iharob Al Asimi Aug 28 '15 at 09:32

2 Answers2

4

No, a mutex is either unnecessary or inappropriate in a signal handler.

In the single-threaded case, the mutex is unnecessary. Yes, you'll want your flag to be volatile, and (for non-trivial access) also sig_atomic_t. However, with only one thread interrupting itself, as it were, you don't need the memory fencing semantics a mutex provides.

In the multithreaded case, by contrast, you do have all the concerns of cache coherency, re-ordering, and variable tearing that mutexes allay. However, no pthread mutex call is async-signal-safe, and thus a mutex cannot be safely manipulated from within a signal handler. You'll need to find another way to mix POSIX threads and signals in this case.

Community
  • 1
  • 1
pilcrow
  • 56,591
  • 13
  • 94
  • 135
3

No, because you are not doing a read-modify-write on flag. You should declare flag as volatile static int flag to prevent the compiler from optimizing away while (flag == 0) to while (1).

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
markgz
  • 6,054
  • 1
  • 19
  • 41
  • 2
    [Someone edited your post](http://stackoverflow.com/revisions/32261918/2) to substantially change your answer, which is a no-no. I rolled back the edit, but I can only agree with what should have been a comment instead of an edit: `sig_atomic_t` is preferred over an `int` because it's guaranteed that reads and writes always see a consistent value. – Filipe Gonçalves Aug 28 '15 at 09:23
  • Thanks Filipe! In practice, reading or writing an aligned `int` is atomic on all modern 32 bit processors. – markgz Aug 28 '15 at 18:12
  • Yeah, it's more a matter of good practice and code clarity I guess. – Filipe Gonçalves Aug 28 '15 at 20:10