[H-GEN] pthreads

Raymond Smith raymonds at uq.net.au
Thu Aug 26 01:29:24 EDT 1999


[ Humbug *General* list - semi-serious discussions about Humbug and
Unix-related topics. ]

Hi Colin, and welcome,

On Thu, 26 Aug 1999, Colin Ward wrote:
> Raymond Smith wrote:
> > You do not really want to do this. If you are using threads I would
> > suggest have a global variable you set in the signal handler and another
> > thread which periodically polls this variable and performs appropriate
> > actions.
> 
> Pardon me, as I am a newcomer around here, but you should *never* use
> variables for signalling that structures are safe to access.

True, I should have said `an appropriate global variable'. But I think we
are speaking at cross purposes: you are talking about signals in a
concurrent programing sense, while I am talking about them in a UNIX IPC
(inter process communication) sense.

UNIX has an idea of their being "processes" which can send signals to one
another. These signals have defined meanings but the process may catch
them with a signal handler and take appropriate action. However, like a
hardware interupt handler, signals are not reliable, and can be lost if
you are already in a handler. Therefore, signal handlers should be short
and fast to execute.

The solution in a single threaded program is to use a flag to register the
signal, and then have a main/select loop take care of it. As you rightly
point out, this may not work in a multi-threaded program because of
multiple threads accesing the variable, unless uit is appropriate
for such access. Andrae has even pointed out which types should be used.

> You use Mutexes and Semaphores for signalling as these will ensure
> that the access is totally atomic for the CPU in use, usually by using
> instructions specific to that CPU for atomic accesses, or disabling
> interrupts.

Yes. (Note: here you are using signalling to mean something more general
than UNIX signals.) A UNIX signal can be thought of as a _software_
interrupt but it is not the same as a _hardware_ interrupt because they
can be generated by any process.

> Well this might be out of line becaue it's not Linux specific, and I
> am a Linux newbie, but it is something which transcends the OS in use. 
> :-)

Indeed. Unfortunately nomenclature is different! If you are beginning to
do 'Linux' programming I strongly suggest you first learn UNIX
programming.

Raymond

Who is becomming a *BSD convert.

---
raymond at humbug.org.au              The early bird catches the worm, 
                                       but the second mouse eats the cheese


--
This is list (humbug) general handled by majordomo at lists.humbug.org.au .
Postings only from subscribed addresses of lists general or general-post.



More information about the General mailing list