This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Is it safe to implement a mutex using sig_atomic_t.


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--;  
    }
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]