On Wed, 29 May 2024 13:17:47 +0200 Bruno Haible wrote: > Takashi Yano wrote: > > To avoid race issues, pthread::once() uses pthread_mutex. This caused > > the handle leak which was fixed by the commit 2c5433e5da82. However, > > this fix introduced another race issue, i.e., the mutex may be used > > after it is destroyed. With this patch, do not use pthread_mutex in > > pthread::once() to avoid both issues. Instead, InterlockedExchage() > > is used. > > This patch is bogus as well, because it allows one thread to return > from a pthread_once call while the other thread is currently > executing the init_routine and not yet done with it. > > > + if (!InterlockedExchange (&once_control->state, 1)) > > + init_routine (); > > return 0; > > } > > There is no code after the init_routine () call here. This means > that other threads are not notified when the init_routine () call > is complete. Therefore this implementation *cannot* be correct. > > See: Assume thread1 and thread2 call pthread_once on the same > once_control. > > thread1 thread2 > ------- ------- > > enters pthread_once enters pthread_once > > sets state to 1 > > sees that state == 1 > > returns from pthread_once > > executes code that assumes > init_routine has completed > > starts executing > init_routine > > finished executing > init_routine > > returns from pthread_once
Thanks for pointing out that. I'll submit a v2 patch. Please have a look. -- Takashi Yano <takashi.y...@nifty.ne.jp>