[184] in Pthreads mailing list archive
Re: API for thread suspend
daemon@ATHENA.MIT.EDU (William S. Gribble)
Sat Oct 28 17:02:21 1995
From: "William S. Gribble" <grib@cs.utexas.edu>
Date: Sat, 28 Oct 1995 15:47:38 -0500
To: ryan@stcs.com.sg
Cc: pthreads@MIT.EDU
In-Reply-To: <9510270831.AA06297@stcs.com.sg> (message from Ryan Wong Hong Yeh on Fri, 27 Oct 95 16:35:13 SST)
I've implemented an API for thread suspend/resume. It's not part of the
POSIX thread spec since it's pretty trivially implementable with what's
there. You need (for each thread) a suspend counter, a condition
variable, and a mutex. You use a signal to the suspended thread,
which will interrupt it and let it put itself to sleep in the
signal handler.
If you look in the C++ class library I posted last week, you can see how
I did this with class PThread, which has suspend() and resume() methods.
You will need to patch PThread.cc to get the class to work right;
replace the constructor with the following:
PThread::PThread(char * thread_name, void (*function)())
: _suspend_count(0),
_thread_function(function)
{
// create the suspend mutex and condition
_suspend_mutex = new PMutex;
_suspend_condition = new PCondition;
// copy the name
strcpy(_thread_name, thread_name);
// create the thread, passing the generic startup routine the
// this pointer
pthread_create(&_pthread, NULL, &thread_start, (void *)this);
}
You'll also need to patch pthread/signal.c in the beta3 or beta4 releases
to fix a signal-delivery problem. The patch is archived in the
pthreads_bugs mailing list (look for my name and the subject line
``small signal patch'' or something similar).
Bill Gribble