On Thu, Sep 12, 2013 at 9:32 AM, K. Frank <[email protected]> wrote:

> Hello Kai and Vadim!
>
> On Wed, Sep 11, 2013 at 10:39 PM, Kai Tietz <> wrote:
> > Hi,
> >
> > 2013/9/12 Vadim <>:
> >> Hi,
> >> I am investigating performance problems in binary compiled with
> mingw-w64,
> >> and libwinpthreads mutex comes at the top of my profile.
> >> Looking at the code, it appears as if mutexes are implemented using
> Windows
> >> semaphores?  Is this right?
> > Yes, that is right.
> >> Does anybody know why critical sections weren't used instead?
> > It is a while ago, but AFAIR this is reasoned by the fact that
> > pthread-mutexes have 3 different flavors.  First recursive,
> > thread-owned, and non-thread-owned.  First and second can
> > beimplemented by a critical section, the third (and standard-variant)
> > can't that easily.  Additional it is required - we didn't implement
> > this feature until now in winpthread - that there are named variants.
> >
> >> Vadim
>
> Just some background comments -- not directly relevant to
> mingw-w64:
>
> Windows semaphores are (as are windows mutexes) interprocess
> synchronization objects, that is, they can be used to synchronize
> different processes, not just different threads of the same process.
>
> As such, they are somewhat heavy weight.
>
> In contrast, windows critical sections are purely intraprocess, so
> they are lighter, and, if you are only working with threads in a single
> process, generally more efficient.
>
> I experimented with using both windows mutexes and windows
> critical sections for implementing std::thread with mingw-w64;
> not surprisingly, the critical-section version was significantly
> faster, at least when it came to creating a lot of std::mutex
> objects.
>

Precisely.  In the absence of contention, critical sections can be up to
sever hundred times faster than kernel mutexes.


>
> (Please note, that my goal was to implement std::thread, not
> pthreads, so the specific needs of pthreads were not relevant
> for my experiment.)
>
> Two down-sides I found with windows critical sections:  They are
> relatively recent -- I think Vista and later.  (I didn't really care
> about this.)


They've been around since win95.   In Vista they were modified such that
InitializeCriticalSection will not fail under low-memory conditions.



> Also, the windows threading api does not support
> a timed wait on a windows critical section (it does on a windows
>

Yes, this might be a problem.



> mutex), so I had to resort to some hackery to get
>

>    std::timed_mutex::try_lock_for()
>
> and related to work with the critical-section implementation.
>
> (Note that std::mutex and std::timed_mutex are different types,
> so, in principle, one could implement std::mutex with windows
> critical sections and std::timed_mutex with windows mutexes.
> But to me it seemed that the extra cost of windows mutexes
> outweighed the hackery needed for the critical-section solution.)
>
> > Kai
>
>
> Happy Pthreads Hacking!
>

Before we embark on that, I'd like to clarify a couple more things:

1. Have you guys considered adopting/forking
phtreads-win32<http://www.sourceware.org/pthreads-win32/>?
I believe this is what the regular mingw uses.  That library looks more
mature, at least as far as mutexes go.
2. Pthreads spec says that performing operations that error out on
PTHREAD_MUTEX_ERRORCHECK, will result in undefined behavior with
PTHREAD_MUTEX_NORMAL.  Since it's undefined, it could as well be returning
an error.  So it seems to me that implementation where PTHREAD_MUTEX_NORMAL
is the same as PTHREAD_MUTEX_ERRORCHECK would be conformant, no?
3. What are the target platforms/compilers for winpthreads?   Is it just
mingw/gcc?

Vadim
------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. Consolidate legacy IT systems to a single system of record for IT
2. Standardize and globalize service processes across IT
3. Implement zero-touch automation to replace manual, redundant tasks
http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to