pthread gotchas

Christopher Faylor
Fri May 18 19:45:00 GMT 2001

On Sat, May 19, 2001 at 12:32:10PM +1000, Robert Collins wrote:
>----- Original Message -----
>From: "Christopher Faylor" <>
>Subject: pthread gotchas
>>Just to be clear up front, the gotchas I'm talking about are my
>>gotchas.  There were apparently some aspects of the way that pthreads
>>work that I was not familiar with.
>Same here.  :]
>>I am looking into finally getting signals working in a multi-threaded
>>enviroment given Corinna's recent problems with cygrunsrv.  So, I wrote
>>a program which forked, started a separate thread, and then called
>>waitpid().  I wanted to see how a SIGINT with a signal handler would
>>work with pthreads.  I ran the program on linux.
>I think delivering signals to threads is broken.  I cannot
>see where you use getthread2signal in  That would
>break waitpid the way you see it broken.
>**** ****

The whole point of my efforts is to get signals working with threads on
cygwin.  I know that they are broken.  The above is just the tip of the
iceberg.  The signal delivery mechanism doesn't even attempt to take
multiple threads into account.  I've tried to carefully make stuff
thread safe but I still have a ways ago to get the signal mechanism
to be able to send signals to any thread other than the main one.

This email was talking about linux.  I ran the program on linux first so
that I could get a feel for how it was supposed to work and was
surprised to find that linux broke my perceptions.

I have included my test program below.  The waitpid, which is invoked in
the secondary thread fails unless I move the fork into the same thread.

I'd still be interested in hearing if there is a good pthreads reference
book which talks about this kind of stuff.


#include <stdio.h>
#include <pthread.h>
#include <wait.h>

static int pid = 0;

static void *
tf (void *v)
  int status;
  printf ("in tf, waiting for %d\n", pid);
  printf ("%d = waitpid\n", status = waitpid (pid, NULL, 0));
  if (status)
    perror ("waitpid");
  puts ("returning");
  return NULL;

main (int argc, char **argv)
  pthread_t tid;
  pthread_attr_t att = {0};
  void *thread_return;

  setbuf (stdout, NULL);
  if ((pid = fork ()) == 0)
      puts ("in subproc, sleeping");
      sleep (10);
      puts ("in subproc, exiting");
      exit (0);

  printf ("in parent, starting thread, pid %d", pid);
  printf ("%d = pthread_create\n", pthread_create (&tid,  NULL, tf, NULL));
  printf ("%d = pthread_join\n", pthread_join (tid, &thread_return));
  puts ("exiting");

More information about the Cygwin-developers mailing list