This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: starvation in pthread_once?


On Thu, 2005-03-03 at 04:32 -0500, Gottlob Frege wrote:
> I'm concerned about the Sleep(0) in pthread_once:
> 

Thanks. It looks like this routine needs to be redesigned.

Regards.
Ross

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]