(转)5.4 双缓冲技术(Double Buffering)-5

下面是PlotSettings的实现:
PlotSettings::PlotSettings()
{
    minX = 0.0;
    maxX = 10.0;
    numXTicks = 5;
    minY = 0.0;
    maxY = 10.0;
    numYTicks = 5;
}
在构造函数中,把两个坐标轴的初始化为范围从0到10,分为5个刻度。
void PlotSettings::scroll(int dx, int dy)
{
    double stepX = spanX() / numXTicks;
    minX += dx * stepX;
    maxX += dx * stepX;
    double stepY = spanY() / numYTicks;
    minY += dy * stepY;
    maxY += dy * stepY;
}
函数scroll()功能:通过两个坐标刻度间的间隔乘以一个指定的偏移量,来增加或者减少minX,maxX,minY,maxY的值。这个函数在Plotter::keyPressEvent()函数中调用,用于实现滚动。
void PlotSettings::adjust()
{
    adjustAxis(minX, maxX, numXTicks);
    adjustAxis(minY, maxY, numYTicks);
}
函数adjust()在Plotter::mouseReleaseEvent()中调用。重新计算minX,maxX,minY,maxY的值,四舍五入到一个恰当的值重新得到坐标轴刻度的个数。私有函数adjustAxis()一次计算一个坐标轴。
void PlotSettings::adjustAxis(double &min, double &max,
                              int &numTicks)
{
    const int MinTicks = 4;
    double grossStep = (max - min) / MinTicks;
    double step = pow(10.0, floor(log10(grossStep)));
    if (5 * step < grossStep) {
        step *= 5;
    } else if (2 * step < grossStep) {
        step *= 2;
    }
    numTicks = int(ceil(max / step) - floor(min / step));
    if (numTicks < MinTicks)
        numTicks = MinTicks;
    min = floor(min / step) * step;
    max = ceil(max / step) * step;
}
函数adjustAxis()转换它的参数min, max为一恰当的值,并根据给定的最大最小范围值计算刻度的个数,设置其参数numTicks为刻度个数。由于该函数需要修改实参的变量值(minX, maxX, numXTicks)而非拷贝,因此未使用常引用(non-const references)。
大部分代码主要是确定坐标轴上两刻度的间隔值(step)。为了得到合理的值,必须得到准确的步长值。例如,一个坐标轴步长为3.8,坐标轴上其他的刻度值都是3.8的倍数,对用户很不习惯,对于一个整数坐标值,合理的步长应给为10n, 2·10n, 或者5·10n
首先我们计算最大步长(gross step),然后计算小于或者等于这个步长的形式为10n数,通过计算这个步长的以十为底的对数向下取整,然后计算这个值的10次方。例如,如果最大步长为236,log (236)为2.37291…,向下取整为2,得到102 = 100作为候选的步长值。
有了第一个值以后,我们再继续计算其他的候选值2·10n 和 5·10n。如上例中,另外两个可能的值为200和500。500大于最大的步长值不能使用,200小于236,使用200作为步长的值。
接着从步长值计算numXTicks,min和max就很容易了。新的min值为(原来的min值/步长)后向下取整和步长的乘积,新的max为(原来的max值/步长)后向上取整和步长的乘积。新的numTicks为(原来的max值/步长)后减去(原来的min值/步长)后向下取整后的间隔数。例如,输入的min值为240,max为1184,新的值就会变成200,1200,200为步长,就有numTicks值为5;
有时这个算法并不是最优的。一个更加复杂的算法是Paul S. Heckbert在Graphics Gem上发表的一篇名为“Nice Numbers for Graph Labels”(ISBN 0-12-286166-3)
这一章是第一部分的最后一章。介绍了怎样从现有的Qt控件基础上自定义一个新的控件,和以QWidget作为基类得到一个新的控件。在第二章我们看到了如何对已有控件进行组合为一个新控件,在第六章中我们将会继续介绍。
到此为止,我们已经介绍了很多Qt GUI编程方面的知识。在第二部分和第三部分中,我们将会深入介绍Qt编程的其他方面。
Preface xv Introduction xvii Mathematical Notation xix Pseudo-Code xxi Contributors xxvi 1 2D GEOMETRY Useful 2D Geometry 3 Trigonometry Summary 12 Useful Trigonometry 13 Trigonometric Functions at Select Points 18 Triangles 20 Generating Random Points in Triangles (649) 24 Fast Line–Edge Intersections on a Uniform Grid (651) 29GRAPHICS GEMS I Edited by ANDREW S. GLASSNER viii CONTENTS Anti-Aliasing Summary 37 Area of Intersection: Circle and a Half-Plane 38 Area of Intersection: Circle and a Thick Line 40 Area of Intersection: Two Circles 43 Vertical Distance from a Point to a Line 47 A Fast 2D Point-on-Line Test (654)49 Fast Circle–Rectangle Intersection Checking (656)51 2 2D RENDERING Circles of Integral Radius on Integer Lattices 57 Nice Numbers for Graph Labels (657)61 Efficient Generation of Sampling Jitter Using Look-up Tables (660)64 Scan Conversion Summary 75 Fast Anti-Aliasing Polygon Scan Conversion (662)76 Generic Convex Polygon Scan Conversion and Clipping (667)84 ConcavePolygon Scan Conversion (681)87 Fast Scan Conversion of Arbitrary Polygons 92 Line-Drawing Summary 98 Digital Line Drawing (685)99 Symmetric Double Step Line Algorithm (686) 101 Rendering Anti-Aliased Lines (690) 105 An Algorithm for Filling in 2D Wide Line Bevel Joints 107 Rendering Fat Lines on a Raster Grid 114GRAPHICS GEMS I Edited by ANDREW S. GLASSNER ix CONTENTS Two-Dimensional Clipping: A Vector-Based Approach (694) 121 Periodic Tilings of the Plane on a Raster 129 3 IMAGE PROCESSING Anti-Aliasing Filters Summary 143 Convenient Anti-Aliasing Filters That Minimize “Bumpy” Sampling 144 Filters for Common Resampling Tasks 147 Smoothing Enlarged Monochrome Images 166 Median Finding on a 3 × 3 Grid (711) 171 Ordered Dithering (713) 176 A Fast Algorithm for General Raster Rotation 179 Useful 1-to-1 Pixel Transforms 196 Alpha Blending 210 4 FRAME BUFFER TECHNIQUES Frame Buffers and Color Maps 215 Reading a Write-Only Write Mask 219 A Digital “Dissolve” Effect (715) 221 Mapping RGB Triples onto Four Bits (718) 233 What Are the Coordinates of a Pixel? 246 Proper Treatment of Pixels as Integers (719) 249 Normal Coding 257 Recording Animation in Binary Order for Progressive Temporal Refinement (720) 265GRAPHICS GEMS I Edited by ANDREW S. GLASSNER x CONTENTS 1-to-1 Pixel Transforms Optimized through Color-Map Manipulation 270 A Seed Fill Algorithm (721) 275 Filling a Region in a Frame Buffer 278 Precalculating Addresses for Fast Fills, Circles, and Lines 285 A Simple Method for Color Quantization: Octree Quantization 287 5 3D GEOMETRY Useful 3D Geometry 297 An Efficient Bounding Sphere (723) 301 Intersection of Two Lines in Three-Space 304 Intersection of Three Planes 305 Mapping Summary 306 Digital Cartography for Computer Graphics 307 Albers Equal-Area Conic Map Projection. (726) 321 Boxes and Spheres Summary 326 Spheres-to-Voxels Conversion 327 A Simple Method for Box-Sphere Intersection Testing (730) 335 6 3D RENDERING 3D Grid Hashing Function (733) 343 Backface Culling 346GRAPHICS GEMS I Edited by ANDREW S. GLASSNER xi CONTENTS Fast Dot Products for Shading 348 Scanline Depth Gradient of a Z-Buffered Triangle 361 Simulating Fog and Haze 364 Interpretation of Texture Map Indices 366 Multidimensional Sum Tables 376 7 RAY TRACING A Simple Ray Rejection Test 385 Ray−Object Intersection Summary 387 Intersection of a Ray with a Sphere 388 An Efficient Ray−Polygon Intersection (735) 390 Fast Ray−Polygon Intersection 394 Fast Ray−Box Intersection (736) 395 Shadow Attenuation for Ray Tracing Transparent Objects 397 8 NUMERICAL AND PROGRAMMING TECHNIQUES Root Finding Summary 403 Cubic and Quartic Roots (738) 404 A Bézier Curve-Based Root-Finder (787) 408 Using Sturm Sequences to Bracket Real Roots of Polynomial Equations (743) 416 Distance Measures Summary 423GRAPHICS GEMS I Edited by ANDREW S. GLASSNER xii CONTENTS A High-Speed, Low Precision Square Root (756) 424 A Fast Approximation to the Hypotenuse (758) 427 A Fast Approximation to 3D Euclidean Distance 432 Full-Precision Constants 434 Converting between Bits and Digits 435 Storage-free Swapping 436 Generating Random Integers 438 Fast 2D−3D Rotation 440 Bit Patterns for Encoding Angles 442 Bit Interleaving for Quad- or Octrees (759) 443 A Fast HSL-to-RGB Transform (763) 448 9 MATRIX TECHNIQUES Matrix Identities 453 Rotation Matrix Methods Summary 455 Transforming Axes 456 Fast Matrix Multiplication 460 A Virtual Trackball 462 Matrix Orthogonalization (765) 464 Rotation Tools 465 Matrix Inversion (766) 470 Matrices and Transformations 472 Efficient Post-Concatenation of Transformation Matrices (770) 476GRAPHICS GEMS I Edited by ANDREW S. GLASSNER xiii CONTENTS 10 MODELING AND TRANSFORMATIONS Transformation Identities 485 Fixed-Point Trigonometry with CORDIC Iterations (773) 494 Using Quaternions for Coding 3D Transformations (775) 498 3D Viewing and Rotation Using Orthonormal Bases (778) 516 The Use of Coordinate Frames in Computer Graphics 522 Forms, Vectors, and Transforms (780) 533 Properties of Surface-Normal Transformations 539 Transforming Axis-Aligned Bounding Boxes (785) 548 Constructing Shapes Summary 551 Defining Surfaces from Sampled Data 552 Defining Surfaces from Contour Data 558 Computing Surface Normals for 3D Models 562 Calculation of Reference Frames along a Space Curve 567 11 CURVES AND SURFACES Planar Cubic Curves 575 Explicit Cubic Spline Interpolation Formulas 579 Fast Spline Drawing 585 Some Properties of Bézier Curves 587 Tutorial on Forward Differencing 594 Integration of Bernstein Basis Functions 604GRAPHICS GEMS I Edited by ANDREW S. GLASSNER xiv CONTENTS Solving the Nearest-Point-on-Curve Problem (787) 607 An Algorithm for Automatically Fitting Digitized Curves (797) 612 References 808 Index
<think>嗯,用户问的是MySQL中的双缓冲机制及其实现方式。首先,我需要确认自己对双缓冲的理解是否正确。双缓冲通常是指在处理数据时使用两个缓冲区,一个用于前台操作,另一个用于后台处理,以减少等待时间并提高效率。这在图形学或者I/O操作中比较常见,但MySQL中的双缓冲可能有所不同。 接下来,我得回忆一下MySQL的相关知识。记得InnoDB存储引擎有缓冲池(Buffer Pool),这是内存中的一块区域,用于缓存数据和索引。可能双缓冲和缓冲池有关?比如在写入数据时,先写入内存缓冲区,再刷盘到磁盘,这样可能涉及到双缓冲机制。 另外,日志系统如redo log和binlog的写入也可能使用双缓冲。比如,redo log的写入过程可能有多个缓冲区,一个正在写入,另一个在准备刷盘,这样可以避免阻塞。或者binlog在同步到从库时,主库可能有一个缓冲区用于累积事件,另一个用于发送,从而实现并行处理。 还需要考虑页面清理线程(Page Cleaner Thread)。当缓冲池中的脏页需要刷盘时,可能使用双缓冲机制来避免阻塞用户线程。比如,一个缓冲区用于继续处理新的写操作,另一个被清理并写入磁盘。 不过,用户提供的参考引用中提到MySQL 8.0的C API用于复制事件流,这可能与binlog的双缓冲有关。比如,主库生成binlog事件时,可能使用两个缓冲区,一个在填充当前事件,另一个用于发送给从库,这样主库可以持续处理事务而不必等待网络传输。 另外,需要确认双缓冲在MySQL中的具体实现是否存在官方文档支持。可能需要查阅MySQL文档或相关源码。比如,InnoDB的缓冲池管理、redo log的写入机制、binlog的组提交等都可能涉及双缓冲技术。 总结一下,可能的实现点包括:缓冲池的脏页写入、redo log的缓冲区切换、binlog的组提交和发送机制,以及复制过程中的双缓冲应用。需要将这些点组织起来,用中文解释清楚,并确保LaTeX格式正确,同时生成相关问题。</think>### MySQL双缓冲机制解析 MySQL的双缓冲机制主要用于优化I/O密集型操作,其核心思想是通过内存缓冲区降低磁盘操作延迟。以下从三个层面分析其工作原理: 1. **InnoDB缓冲池的双层管理** - 主缓冲池($Buffer\ Pool$)采用分段LRU算法管理数据页 - 写操作先修改内存中的脏页($dirty\ page$),后台线程异步刷盘 - 通过$doublewrite\ buffer$实现安全页写入: $$page_{memory} \xrightarrow{flush} doublewrite\ buffer \xrightarrow{fsync} data\ file$$ 2. **日志系统的缓冲设计** - Redo log使用循环双缓冲区: ```mermaid graph LR A[活跃缓冲区] -->|写满后切换| B[备用缓冲区] B -->|后台刷盘| C[(磁盘)] ``` - Binlog通过组提交($Group\ Commit$)优化: $$事务1+事务2+... \xrightarrow{缓冲} 批量fsync$$ 3. **复制流程中的缓冲应用** - 主库使用$binlog\ cache$临时存储事件[^1] - 从库通过$relay\ log\ buffer$进行中继缓冲 - 并行复制时采用多工作线程+缓冲队列模式 ### 实现关键点 - 内存屏障保证缓冲区切换原子性 - 刷盘阈值动态调整($innodb_flush_neighbors$) - 异步I/O线程池管理($innodb_write_io_threads$)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值