Thread: alarm() and threads

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    alarm() and threads

    does alarm() work on a per-thread or per-process basis? for example, can each thread in a given process have an independent alarm timer, or does the whole process only get one?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elkvis View Post
    does alarm() work on a per-thread or per-process basis? for example, can each thread in a given process have an independent alarm timer, or does the whole process only get one?
    alarm() is signal-based, and signals are per-process. You can control which thread receives a signal to a LIMITED degree. Each thread has its own signal mask. If you block SIGALRM in all threads but one, then that one thread will be the one which receives the signal. If more than one threads has the signal unblocked, it is unspecified which one will receive the signal.

    What it boils down to is that there is no easy way to use alarm() in a multi-threaded environment to create simultaneous alarms for multiple threads.

    In fact, because of how POSIX defines the signal/thread interactions, signals are rendered pretty much useless for ANY thread-specific uses. They really are a per-process control mechanism.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Internal Workings Of The = Operator?
    By Geolingo in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2003, 11:07 PM
  3. Results - 4th contest, saturday, august 11
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 08-12-2002, 09:41 AM