This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Is it safe to implement a mutex using sig_atomic_t.
- From: mishabear <mishabear at gmail dot com>
- To: libc-alpha at sources dot redhat dot com
- Date: Fri, 1 Jul 2005 16:44:00 +0800
- Subject: Is it safe to implement a mutex using sig_atomic_t.
- Reply-to: mishabear <mishabear at gmail dot com>
I have modified the "Non-atomic example" of Atomic Data Access. The
result seems fine. But I'm not sure whether it is safe to implement a
mutex using sig_atomic_t.
#include <signal.h>
#include <stdio.h>
sig_atomic_t mtx;
struct two_words { int a, b; } memory;
void handler(int signum)
{
if(++mtx == 1)
{
printf ("%d,%d\n", memory.a, memory.b);
}
mtx--;
alarm (1);
}
int main (void)
{
static struct two_words zeros = { 0, 0 }, ones = { 1, 1 };
signal (SIGALRM, handler);
mtx = 0;
memory = zeros;
alarm (1);
while (1)
{
if(++mtx == 1)
{
memory = zeros;
memory = ones;
}
mtx--;
}
}