Qt文档阅读笔记-隐式共享(Implicit Sharing)深入研究(理论及实例)
发布日期:2021-06-30 10:43:06 浏览次数:3 分类:技术文章

本文共 6175 字,大约阅读时间需要 20 分钟。

Qt里面很多C++类都是采用隐式共享最大限度的提高资源利用率以及最新复制的原则。隐式共享数据作为参数时,不仅安全而且高效,因为传的是一个指针并且只有要修改这个数据时才会去拷贝,这里有个专业词汇copy-on-write在Qt官方提供的ppt中经常会出现这个词,说的就是隐式共享。

 

共享类(这个应该是指value type的类,学过Qt的都知道,Qt类可以分为两类,一个是identity type和value type)由一个共享数据块及指向这个块的计数器组成。

当共享对象被创建,这个块的计数器的值会被设置为1,并且当有新的对象指向共享数据库,那么他的计数器就会+1,当对象不指向这个内存块时,计数器会减少,当计数器为0时,这个共享块将会被删除。

当要对共享数据进行处理时,Qt有2种方式进行拷贝,也就是常说的深拷贝与浅拷贝。这里不再解释这2个拷贝了,这个隐式共享就和浅拷贝差不多,只不过是多了给增加了个引用计数器。

=这个操作一般是浅拷贝。

 

Qt的容器iterator和STL的不同,主要也是围绕隐式共享。如下代码:

#include 
#include
int main(int argc, char *argv[]){ QVector
a, b; a.resize(100000); QVector
::iterator i = a.begin(); b = a; *i = 4; a[0] = 5; b.clear(); int j = *i; qDebug() << "OVER";}

下面最关键的来了,演示下上面所说的理论

这里可以看到当*i = 4还没执行的时候a中RefCount为2,也就是a,b都引用到了这个隐式内存中,所以为2。而这个i是直接指向了目前a最开始的那个内存。

下面看下一张图:

这里可以看到*i=4改变后,因为改变的是共享内存,所有a和b里面的数据都被改了。

最关键的地方来了,大家要注意,Qt最强大的机制之一copy-on-write来了。

这里a[0]=5,写及拷贝。a重新进行构造,现在RefCount又变成1了。

这个程序想表示的就是这个。也就是Qt很重要的运行机制。

还有给要提的就是这个i。

当b被clear后这个i就有问题,int j = i;这种操作很不安全。

 

当开发者需要写自己的隐式共享类时,使用QSharedData和QSharedDataPointer类。

 

当需要改变数据的时候,数据将会从隐式共享分离。

如下面这个QPen类,内部是这样处理的

void QPen::setStyle(Qt::PenStyle style)  {      detach();           // detach from common data      d->style = style;   // set the style member  }  void QPen::detach()  {      if (d->ref != 1) {          ...             // perform a deep copy      }  }

当setStyle被调用后,这个set为设置,所以改变了,就会调用detach(),当d-ref不为1时,就会把所有数据拷贝一份。

 

下面是一些隐式共享的类,这里要记住,QTL的容器如果用STL风格去操作,需要考虑下隐式共享方面的问题。

Output stream for debugging information

Access to directory structures and their contents

System-independent file information

Holds the environment variables that can be passed to a program

Provides information about currently mounted storage and drives

Convenient interface for working with URLs

Way to manipulate a key-value pairs in a URL's query

Used to locate data in a data model

Encapsulates a JSON array

Way to read and write JSON documents

Used to report errors during JSON parsing

Encapsulates a JSON object

Encapsulates a value in JSON

Acts like a union for the most common Qt data types

Describes types of file or data, represented by a MIME type string

Array of bits

Array of bytes

List of byte arrays

Template class that provides a cache

Compares strings according to a localized collation algorithm

Can be used to speed up string collation

Defines a possible command-line option

Template class that provides a contiguous cache

Date and time functions

Template class that provides a hash-table-based dictionary

Convenience QHash subclass that provides multi-valued hashes

Template class that provides linked lists

Template class that provides lists

Converts between numbers and their string representations in various languages

Template class that provides a red-black-tree-based dictionary

Convenience QMap subclass that provides multi-valued maps

Generic container that provides a queue

Pattern matching using regular expressions

Pattern matching using regular expressions

The results of matching a QRegularExpression against a string

Iterator on the results of a global match of a QRegularExpression object against a string

Template class that provides a hash-table-based set

Template class that provides a stack

Unicode character string

List of strings

Way of finding Unicode text boundaries in a string

Template class that provides a dynamic array

Refers to one pending asynchronous call

Holds one Unix file descriptor

Monochrome (1-bit depth) pixmaps

Scalable icons in different modes and states

Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device

Paint device that records and replays QPainter commands

Off-screen image representation that can be used as a paint device

Mouse cursor with an arbitrary shape

Encapsulates a key sequence as used by shortcuts

Contains color groups for each widget state

Wraps an OpenGL debug message

Defines the fill pattern of shapes drawn by QPainter

Used in combination with QBrush to specify gradient fills

Container for painting operations, enabling graphical shapes to be constructed and reused

Defines how a QPainter should draw lines and outlines of shapes

Vector of points using integer precision

Vector of points using floating point precision

Specifies a clip region for a painter

Specifies a font used for drawing text

General information about fonts

Font metrics information

Font metrics information

Direct access to the internal glyphs in a font

Access to a single physical instance of a font

Enables optimized drawing of text when the text and its layout is updated rarely

Offers an API to access and modify QTextDocuments

Represents a piece of formatted text from a QTextDocument

Formatting information for blocks of text in a QTextDocument

Formatting information for characters in a QTextDocument

Formatting information for a QTextDocument

Formatting information for frames in a QTextDocument

Formatting information for images in a QTextDocument

Formatting information for lists in a QTextDocument

Formatting information for table cells in a QTextDocument

Formatting information for tables in a QTextDocument

Cache information

Holds a body part to be used inside a HTTP multipart MIME message

Holds one network cookie

Holds a request to be sent with QNetworkAccessManager

Abstraction of one or more access point configurations

Stores information about a domain name record

Stores information about a host address record

Stores information about a DNS MX record

Stores information about a DNS SRV record

Stores information about a DNS TXT record

Stores one IP address supported by a network interface, along with its associated netmask and broadcast address

Listing of the host's IP addresses and network interfaces

Network layer proxy

Used to query the proxy settings for a socket

Convenient API for an X509 certificate

API for accessing the extensions of an X509 certificate

Represents an SSL cryptographic cipher

Holds the configuration and state of an SSL connection

Interface for Diffie-Hellman parameters for servers

SSL error

Interface for private and public keys

Authentication data for pre shared keys (PSK) ciphersuites

转载地址:https://it1995.blog.csdn.net/article/details/106070812 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Web前端笔记-使用@media(媒体查询)展示及隐藏div
下一篇:Qt工作笔记-profile中INSTALLS的使用

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月25日 07时25分37秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章