This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
RE: Mutex implementation questions
- To: 'Tristan Savatier' <tristan at mpegtv dot com>
- Subject: RE: Mutex implementation questions
- From: "Bossom, John" <John dot Bossom at Cognos dot COM>
- Date: Tue, 3 Oct 2000 07:17:12 -0400
- Cc: "'pthreads-win32 at sources dot redhat dot com'" <pthreads-win32 at sources dot redhat dot com>
You are correct... critical sections are almost 100 times faster than
actual kernel based mutexes... however...
1) Mutexes (i.e. kernel based) are useful in that you can support a
lock-with-timeout... a feature that is being pondered as an upgrade
to pthreads.
2) Mutexes can use WaitForMultipleObjects (sp?) which allows you to simulate
a cancel by waiting on a cancel event as well as the mutex.
3) TryEnterCriticalSection is not supported on Win95/98 (originally it was
completely left out... then they added a stub that simply returns
"function not supported", thus there is no way to implement
pthread_mutex_trylock; hence if you need "trylock" you need to use a
real mutex.
3) By eliminating the DllMain, whose purpose was to perform
behind the scenes cleanup of the "self" structure as well as calling
all the destructors for thread-specific data keys, what alternate
approach did you take for doing this thread-based cleanup?
(i.e. I leveraged the fact that DllMain is called each time a thread
is started/finished to perform thread based initialization/cleanup)
I hope this clears up some of your questions.
Cheers,
John.
-----Original Message-----
From: Tristan Savatier [mailto:tristan@mpegtv.com]
Sent: Monday, October 02, 2000 6:10 PM
To: 'pthreads-win32@sources.redhat.com'
Subject: Mutex implementation questions
I noticed that if _pthread_try_enter_critical_section has been set
to a non-NULL value by DllMain, Mutexes are implemented using
critical sections (InitializeCriticalSection) instead of
CreateMutex, regardless of the value of the implemetation-specific
forcecs
mutex attribute.
According to "Win32 programming", critical sections are light weight
compared to mutexes, they are not managed by the kernel, and they
are much faster than mutexes. So why no use critical sections
all the time to implement pthread mutexes ?
Are there cases where critical sections are not available or
wouldn't work well ?
The DllMain seems to do some tests to check if InitializeCriticalSection
exists and works. What are the cases where critical sections
would not be available ?
Also, I have a suggestion:
It might be a good idea to add a compile flag to
allow the use of pthread-win32 with static linking
(i.e. to make just a pthread.lib, no dll).
In this case, a compilation flag should be added to exclude the DllMain
routine. Also, the code that sets _pthread_try_enter_critical_section
should be moved from DllMain to _pthread_processInitialize. The
_pthread_processInitialize should be made global and it should
be called by the application to initialize pthread.
We had to change the pthread code to do all that in our
WinCE application.
-t