1 Replies - 577 Views - Last Post: 30 October 2012 - 06:58 PM Rate Topic: -----

#1 smiths39  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-October 12

posix threads block signal and unblock

Posted 30 October 2012 - 06:56 PM

Is there a way to block certain signals and unblock other signals in the same set? I just don't seem to get my head around it!

I am creating two threads, if SIGUSR1 arrives 'printer_thread1' will print something, if SIGUSR2 arrives 'printer_thread2' will print something. I am adding both signals to the set and I don't know how to block one and unblock the other (and vice versa)

An example
sigset_t set;
sigemptyset(&set);

sigaddset(&set, SIGUSR1);
// Block signal SIGUSR1 in this thread
pthread_sigmask(SIG_BLOCK, &set, NULL);
sigaddset(&set, SIGUSR2);
// Listen to signal SIGUSR2
pthread_sigmask(SIG_UNBLOCK, &set, NULL);


pthread_t printer_thread1, printer_thread2;
pthread_create(&printer_thread1, NULL, print, (void *)&f1);
pthread_create(&printer_thread2, NULL, print, (void *)&f2);

bool tl = true;
while(1)
{
    if(tl)
    {
        // thread1 does something
        kill(pid, SIGUSR1);
        // main thread waits for SIGALRM
        sigwait(&set, &sig);
        tl = !tl;
    }
    else
    {
        // thread2 does something
        kill(pid, SIGUSR2);
        // main thread waits for SIGALRM
        sigwait(&set, &sig);
        tl = !tl;
    }
}



I am not allowed to use Mutexs, semaphores, etc only signals.

Can someone help? :)

Is This A Good Question/Topic? 0
  • +

Replies To: posix threads block signal and unblock

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,269
  • Joined: 27-December 08

Re: posix threads block signal and unblock

Posted 30 October 2012 - 06:58 PM

Moved to C/C++. Please do not post help questions in the Challenges forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1