[Qt-interest] QAtomicInt and QThread

Thiago Macieira thiago at kde.org
Fri Jun 24 17:50:16 CEST 2011


Em Friday, 24 de June de 2011, às 12:41:43, Francisco Gonzalez escreveu:
> 2011/6/24 Thiago Macieira <thiago at kde.org>:
> > On Friday, 24 de June de 2011 10:46:44 Francisco Gonzalez wrote:
> >> this example compiles and run ok, but is it correct?
> > 
> > Maybe it is. We don't know what you're trying to do.
> > 
> > We can't tell if it is correct or not if we don't know what it's supposed
> > to do and what the conditions are. Please explain them too.
> 
> Definitively the example is not good.
> The questions were: Does all the methods and data of "object" live in
> the same thread after doing    "object.moveToThread(&thread);"

I think you don't understand what "live" means.

The object is now affined to that thread. That means QObject will only allow you 
to use its internals from that thread. Your own data follows your own rules.

Code doesn't belong to any thread.

> If it is the case, as we use methods to retrieve data from the object
> we do not need to block to be safe?

You do need a lock to be safe.

> So in this case the read() method will be safe?

No, it won't. The reading of that 64-bit integer is very, very dangerous.

> If we want to modify shared data directly, may we use QAtomicInt to
> block the part of the code that is writing on the code?

QAtomicInt is not a lock. It protects only itself, and only if you use it 
correctly, using the atomic methods. You didn't do any of those three things.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110624/bfc379cd/attachment.bin 


More information about the Qt-interest-old mailing list