Search the web
Sign In
New User? Sign Up
boost · C++ Boost
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Boost.Threads - once functions   Message List  
Reply | Forward Message #15442 of 27136 |
Re: Boost.Threads - once functions


> > POSIX uses pthread_once to do this,
> > but there's no Win32 equivalent. Instead, Win32 programmers are
> > expected to make use of DllMain to handle this sort of
> initialization
> in Win32 pthread_once can be simulated by using something similar to
> the the following:
>
> struct OnceInfo {
> DWORD counter;
> bool done;
> };
>
> static OnceInfo info = {-1,false};
>
> void doOnce(OnceInfo& info,void (*foo)())
> {
> if (InterlockedIncrement(&info.counter)==0) {
> foo();
> info.done=true;
> } else while (!info.done) Sleep(0);
> }

without initial check of info.done you could end up
calling foo() multiple times. that is incorrect.
also, Interlocked stuff does not formally provide
memory synch guaranties (does IA64 windows version
have "memory fence" build into it ??) i would really
try to avoid it.

another problem is that you misuse scheduler
to wait for something. this is really dangerous.
consider the situation when a lower priority thread
gets preempted in favor of some higher priority thread(s)
(on uniprocessor with priority scheduling) while it was
still busy inside your "critical section"; your while loop
may never end; deadlock.

consider the following example (just an illustration;
no error checking, etc):

int process_private_once_status = 0;
int __declspec(thread) thread_local_once_status = 0;

void dcl_once()
{

// 1st check
if ( 0 == thread_local_once_status ) {

// Create/Open named mutex
HANDLE hndlMutex = CreateMutex( NULL,
FALSE,
ONCE_MUTEX_NAME(
process_private_once_status ) );

// Synchronize r/w access to process_private_once_status variable
WaitForSingleObject( hndlMutex,INFINITE );

// 2nd check
if ( 0 == process_private_once_status ) {

// once
myfunc();

// once done
process_private_once_status = 1;

}

// Mutex cleanup
ReleaseMutex( hndlMutex );
CloseHandle( hndlMutex );

// Done for this thread
thread_local_once_status = 1;

}

}

it does not use poorly defined (with respect to memory synch)
Interlocked stuff; it follows PORTABLE memory synchronization
rules using a mutex which is also used for proper "waiting".

regards,
alexander.

ps. __declspec(thread):

"When you use the compiler directive __declspec(thread),
the data that you define doesn't go into either the
.data or .bss sections. It ends up in the .tls section,
which refers to "thread local storage," and is related
to the TlsAlloc family of Win32 functions. When dealing
with a .tls section, the memory manager sets up the
page tables so that whenever a process switches threads,
a new set of physical memory pages is mapped to the
.tls section's address space. This permits per-thread
global variables. In most cases, it is much easier to
use this mechanism than to allocate memory on a
per-thread basis and store its pointer in a
TlsAlloc'ed slot."






Wed Aug 1, 2001 9:53 am

terekhov@...
Send Email Send Email

Forward
Message #15442 of 27136 |
Expand Messages Author Sort by Date

I've been reworking the tss type in Boost.Threads to be type safe (no void*) and manage the data lifetimes internally. This is a royal pain on Win32 platforms...
williamkempf@...
Send Email
Jul 31, 2001
8:57 pm

... how about some generic singleton template. ideally, in thread-aware C++200x ;) it would be really great to be able to write something like: Singleton&...
Alexander Terekhov
terekhov@...
Send Email
Jul 31, 2001
11:11 pm

... initialization ... access ... this, ... initialization ... and ... interface ... A singleton is not the same thing as what we're talking about here (though...
williamkempf@...
Send Email
Aug 1, 2001
2:00 pm

... initialization in Win32 pthread_once can be simulated by using something similar to the the following: struct OnceInfo { DWORD counter; bool done; }; ...
tneumann@...
Send Email
Aug 1, 2001
7:16 am

... to ... I know there's ways to simulate the functionality. Other wise the question wouldn't have been posed for whether or not Boost.Threads should have...
williamkempf@...
Send Email
Aug 1, 2001
2:11 pm

... without initial check of info.done you could end up calling foo() multiple times. that is incorrect. also, Interlocked stuff does not formally provide ...
Alexander Terekhov
terekhov@...
Send Email
Aug 1, 2001
9:53 am

... to ... I never thought of using a named mutex approach. Not overly efficient, but a very nice idea non-the-less. I've been thinking too much about...
williamkempf@...
Send Email
Aug 1, 2001
2:31 pm

... yeah.. the problem of (need for) thread-safe lazy-initialization (which works perfectly with static locals in single threaded C/C++ programs) has nothing...
Alexander Terekhov
terekhov@...
Send Email
Aug 1, 2001
2:37 pm

... how about "ReleaseMutex(mutex)". also, named mutex need to be associated with &once (once control variable). that is why i was using macro/function...
Alexander Terekhov
terekhov@...
Send Email
Aug 1, 2001
10:13 pm

... too ... Accidently left out of the code. That's what you get when you code in e-mails ;). ... No, it doesn't "need" to be. With it not being associated...
williamkempf@...
Send Email
Aug 1, 2001
11:46 pm

... one "global" mutex would result in rather excessive synchronization (it could hurt; especially at system IPL time) perhaps i was not clear enough.. it does...
Alexander Terekhov
terekhov@...
Send Email
Aug 2, 2001
12:06 pm

... &once ... or ... intraprocess ... I'll agree with all that, though "need" is the wrong word ;). You're talking optimizations here, which are very...
williamkempf@...
Send Email
Aug 2, 2001
1:51 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help