1

In the Qt Synchronizing threads documentation listed here:

http://doc.qt.io/qt-5/threads-synchronizing.html

They wrote:

Note: Qt's synchronization classes rely on the use of properly aligned pointers. For instance, you cannot use packed classes with MSVC.

The sentence is not clear.

Which are the limitations of using Qt Synchronization classes with MSVC compiler?

ProEns08
  • 1,856
  • 2
  • 22
  • 38
  • 1
    If you don't use "packed classes" you should be OK to use them. Packed classes is where you manipulate the struct alignment. If you don't change anything here you are probably just fine. – Hayt Aug 31 '16 at 13:33
  • @Hayt: thanks, +1. You can write a complete answer. I will upvote it. – ProEns08 Aug 31 '16 at 13:34

3 Answers3

3

You're facing a documentation bug, with following fix:

Note: Qt's synchronization classes rely on the use of properly aligned pointers. For instance, you cannot use t̲h̲e̲m̲ i̲n̲ packed classes with MSVC.

MSVC is a red herring. Qt's synchronization classes don't work with packed structures period, on all platforms Qt is supported on - since all these platforms support packed structures, and know how to access unaligned members of such structures.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
2

It should not be an issue if you don't use packed classes (if you don't know what that is you are likely not to be using them).

See here for some information about what they are: Class contiguous data

Community
  • 1
  • 1
Hayt
  • 5,210
  • 30
  • 37
  • Actually, packed classes are just one of the examples that could cause a failure. Therefore, avoiding packed classes is necessary but not sufficient. – MSalters Aug 31 '16 at 15:14
2

Alignment refers to a restriction on the addresses of objects of certain types. For the compilers supported by Qt, there's just one sort of alignment restriction: some addresses should be a multiple of 2,4 or 8.

There are a few ways in which this restriction can be violated. In a packed class, when you have a float followed by a char followed by another float there will be no gaps between the three members (that's why they're called packed). As a result, the second float has an address that's 5 higher than the first. It's fairly obvious that one of the two addresses is not a multiple of 4 (the alignment of float).

Another way in which this can occur is casting a random char* to float*. The last two bits of the char* should be zero in this case.

MSVC++ can deal with such unaligned data (it's just slightly slower), but it does so by having the CPU load the data in two operations. This breaks Qt's synchronization which assumes that data is loaded in one operation, such that you get either an old or a new value. If the load is split in two operations, the first may see an old value and the second a new value. The result is that the register contains a mix of old and new bits (!)

MSalters
  • 173,980
  • 10
  • 155
  • 350