[Development] QMutex with pthread on Linux

Thiago Macieira thiago.macieira at intel.com
Sun Aug 26 12:23:03 CEST 2012


On domingo, 26 de agosto de 2012 10.45.31, Olivier Goffart wrote:
> Keep in mind that the micro benchmark really stress the mutexes, and is not
> really realistic: The benchmark do nothing outside the lock.
> In a typical scenario, if you use multiple thread, it is because you want to
> paralellize something.
> In that benchmark, a lot of pressure is put on the freelist and the loop
> inside lockInternal. But in a normal case, in which the number of operation
> outside of the lock is significant, there shoud be much less pressure on
> this loop, and the testAndSet fails much less often.

True, that's the problem of micro-benchmarking. As I said in the introduction, 
low contention rates have very little overhead, and even the msleep(0) is 
worse only by 5%.

> Also, maybe one could also optimize the pthread code we use by using
> semaphore instead. Or maybe just mutexes and no waitcondition. The problem
> is that pthread mutex can't do timed lock, so we need to be smart in order
> to allow tryLock(timeout).  (We could try to do it with a wait condition
> only if we detect the mutex has been used with a timeout)

I scrapped the qthread_unix.cpp code and rewrote it to implement a semaphore, 
but in the end my design ended up exactly like the existing one. I haven't yet 
benchmarked sem_t (unnamed mode). But even if sem_t is faster on Linux, the 
same might not hold on other Unix, which is where this code is really used.

We need that the other Unix systems (non-Mac, non-Linux) benchmark sem_t and 
semaphores via pthread_mutex&cond so a decision can be made. Or, better yet, 
provide us with a low-level semaphore like the other platforms -- preferably 
one that doesn't require construction or destruction.

By the way, we need this NOW. If the structure for the semaphore requires more 
than one pointer, we'll be stuck until Qt 6.0.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
     Intel Sweden AB - Registration Number: 556189-6027
     Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/development/attachments/20120826/232423da/attachment.sig>


More information about the Development mailing list