Subj : Re: How to manage time-slicing in Pthreads programs? To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Sun Feb 20 2005 04:43 pm "Joe Seigh" writes: >> How would you design a mechanism which allows the user to press ^C to >> abort a program? Or set up a timer for a userspace thread scheduler? >> >> I'm not saying that I like signals, but I don't know how an alternative >> could look like. > > It would look like the synchronization mechanisms in pthreads or > built on top of it. How? The program wants some action to be executed when the user presses ^C or when a timer expires. How the action would be executed? In a newly created thread? What would it use to notify the main thread that it should abort what it is currently doing in some convenient place? I implemented a runtime for a high level language. A signal handler changes volatile variables which fake the stack overflow condition (I mean my stack, not system stack) for the thread which receives signals (I mean my thread, not pthread). The stack overflow handler checks whether the real cause was a Unix signal to be translated to a signal of my language, or maybe a timer tick. This way the only extra overhead caused by Unix signals and the timer is that variables for stack limits are volatile. (I violate the C standard by sharing with a signal handler variables of other types than sig_atomic_t, in particular pointers. I don't care as long as it works on systems I'm interested in. If there is an important system where it doesn't work, I will #ifdef an alternative implementation.) If an asynchronous event caused another thread to be created, it would have to synchronize with the main thread using a variable protected by a mutex. This means locking a mutex on each place where I currently check for stack overflow. This is more expensive. Since the Unix signal handler runs in the context of the thread which has been interrupted, it doesn't need locks or memory barriers, it only needs volatiles. I'm happy that it doesn't need expensive synchronization. If you don't mean running the handler in its own thread, then what? > Also the windows synchronization api which doesn't have signals. What does it use for ^C and timers? How is the timer action executed? > The fact that it is nearly impossible to use unix signals correctly > in threaded programs without using sigwaitinfo() or sigtimedwait() > should be a clue. I believe my runtime is correct (it almost doesn't use pthreads). Anyway, it is not a clue until we have a better mechanism. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .