pthread gotchas

Robert Collins
Fri May 18 19:33:00 GMT 2001

----- Original Message -----
From: "Christopher Faylor" <>
Subject: pthread gotchas

> (I'll bet that this got Robert's attention)

Nope not at all. :]

> Just to be clear up front, the gotchas I'm talking about are my
> There were apparently some aspects of the way that pthreads work that
> 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.

Let me see if I understand this correctly: foo(pid,procthreadid)
foo(1,1) starts
foo(1,1) forks, giving foo(2,1)
foo(1,1) starts a new thread foo(1,2)
foo(1,2) calls waitpid(2,&loc,opt)

that should work. (Implied by two things: it's a process child that
matters, and multiple threads are allowed to waitpid on the same child

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.
**** ****

> I was surprised to find out that waitpid failed in this scenario.
> Apparently this was due to the fact that fork was called in the
> main thread and waitpid was called in a secondary thread.

Well, a bug in cygwin ;]

> I hadn't anticipated this behavior when I wrote the process handling
> years ago, even though I thought I was cleverly making things thread
> safe.
> So, my question is, is this standard pthread behavior?  I know that
> linux's pthread implementation has been through a couple of
> I don't know if it is fully compliant or not.

I don't believe so. I think that this is wrong.

> Is there other stuff that will work this way as well?  For instance,
> will strtok (not strtok_r) work right?  I'm wonder how many of my
> misguided perceptions I need to examine.

strtok is not guaranteed threadsafe. It should never be used within
cygwin, and never used by multi-threaded applications.

> I've searched the net for a pthreads tutorial but the only ones I've
> been able to find are really basic.  Does anyone have a recommendation
> for a good reference?

Uhmm, writing a pthread library seems to be the qualifier :]

> cgf

More information about the Cygwin-developers mailing list