alarm() and threads

This is a discussion on alarm() and threads within the Linux Programming forums, part of the Platform Specific Boards category; does alarm() work on a per-thread or per-process basis? for example, can each thread in a given process have an ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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, 08:56 AM
  2. Internal Workings Of The = Operator?
    By DeepBlackMagic in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2003, 12:07 AM
  3. Results - 4th contest, saturday, august 11
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 08-12-2002, 10:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21