[Qt-interest] How to make a variable (primitive, object or pointer to something) thread safe ?

Arnold Krille arnold at arnoldarts.de
Sun Aug 23 22:29:09 CEST 2009


Hi,

On Sunday 23 August 2009 11:58:25 Sean Harmer wrote:
> On Sunday 23 Aug 2009 10:08:34 Patric wrote:
> > Hi guys,
> > is there a way to make a class member (primitive variable, an object or
> > just a pointer) thread safe ? I know that QMutex is used to synchronize
> > some code block, but is there any way to do it for a single variable ?
> Think of the mutex as protecting the data in the code block not as
> protecting the code block. It then follows that you need to use your mutex
> everywhere that you can read or write to your variable. Take a look at the
> mandelbrot example for a typical pattern of protecting the member variables
> of a class. Notice how in the run() function the member variables are
> copied into local variables (whilst holding the mutex). This is so that the
> mutex can be unlocked as soon as possible so as not to block the main
> thread.

Next step would then be to use QReadWriteLock (and QReadLocker/QWriteLocker) 
to distinguish between const-functions only reading the data and non-const to 
modify the data. Add implicit or explicit sharing with 
Q(Explicitely)SharedDataPointer and you have what I implemented last week...

Have fun,

rnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090823/e5bbe821/attachment.bin 


More information about the Qt-interest-old mailing list