public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* starvation in pthread_once?
@ 2005-03-03  9:32 Gottlob Frege
  2005-03-04 23:18 ` Ross Johnson
  0 siblings, 1 reply; 15+ messages in thread
From: Gottlob Frege @ 2005-03-03  9:32 UTC (permalink / raw)
  To: pthreads-win32

I'm concerned about the Sleep(0) in pthread_once:

  if (!once_control->done)
    {
      if (InterlockedIncrement (&(once_control->started)) == 0)
        {
          /*
           * First thread to increment the started variable
           */
          (*init_routine) ();
          once_control->done = PTW32_TRUE;

        }
      else
        {
          /*
           * Block until other thread finishes executing the onceRoutine
           */
          while (!(once_control->done))
            {
              /*
               * The following gives up CPU cycles without pausing
               * unnecessarily
               */
              Sleep (0);
            }
        }
    }

IIRC, Sleep(0) does not relinquish time slices to lower priority
threads.  (Sleep(n) for n != 0 does, but 0 does not).  So, if a lower
priority thread is first in, followed closely by a higher priority
one, the higher priority thread will spin on Sleep(0) *forever*
because the lower, first thread will never get a chance to set done.

So even Sleep(10) should be good enough.  In theory, there could be
enough higher priority threads in the system that the first thread
still doesn't get in (ever?!), but unlikely.  And that would probably
mean a general design flaw of the calling code, not pthread_once.

?

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2009-09-17  1:28 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-03  9:32 starvation in pthread_once? Gottlob Frege
2005-03-04 23:18 ` Ross Johnson
2005-03-08  2:22   ` Ross Johnson
2005-03-08  3:00     ` Gottlob Frege
2005-03-08  6:11       ` Ross Johnson
2005-03-08  9:49         ` Alexander Terekhov
2005-03-08  9:56           ` Alexander Terekhov
2005-03-08  9:58             ` Alexander Terekhov
2005-03-08 16:11               ` Gottlob Frege
2005-03-08 17:14                 ` Alexander Terekhov
2005-03-08 18:28                   ` Gottlob Frege
2005-03-14  2:47                 ` Ross Johnson
     [not found]                   ` <97ffb310503140832401faa2b@mail.gmail.com>
     [not found]                     ` <1110842168.21321.78.camel@desk.home>
     [not found]                       ` <97ffb3105031415473a3ee169@mail.gmail.com>
     [not found]                         ` <1110855601.21321.203.camel@desk.home>
     [not found]                           ` <97ffb31050321080747aa5a7c@mail.gmail.com>
     [not found]                             ` <1111464847.8363.91.camel@desk.home>
     [not found]                               ` <97ffb3105032209269b0f44e@mail.gmail.com>
2009-09-16 15:38                                 ` Gottlob Frege
2009-09-17  1:28                                   ` Ross Johnson
2005-03-08 16:05       ` Gottlob Frege

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).