⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsearraytest.cpp

📁 window下的多线程编程参考书。值得一读
💻 CPP
字号:
//
// FILE: SparseArrayTest.cpp
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////

#include "SparseArray.h"

// This program instantiates a very large array (one that takes
// up 20 pages of memory) and then does some reads and writes
// to that array to simulate sparse utilization.
//
void main( void )
{
    // Figure out how big a page is.
    //
    SYSTEM_INFO SystemInfo;

    GetSystemInfo(&SystemInfo);

    DWORD dwPageSize = SystemInfo.dwPageSize;

    // We'll instantiate an array that's 20 pages worth of
    // BYTEs, and specify an "undefined element" of '?'.
    //
    long lNumElements = ((20 * dwPageSize) / sizeof(BYTE));
    long lFirstValidIndex = 0;
    long lLastValidIndex = (lNumElements * sizeof(BYTE)) - 1;

    // Here's the array.
    //
    CSparseArray<BYTE>  ReallyBigArray(lNumElements, '?');

    // We'll perform 10 writes to the array at random indices to
    // simulate sparse access to the array.
    //
    srand(GetCurrentThreadId());

    for( BYTE bWriteCount = 0; bWriteCount < 10; bWriteCount++ )
    {
        // To illustrate what's going on, we'll predict what page
        // we're going to write to so that you can see which writes
        // cause a new page to be committed.
        //
        long lElementIndex = (long)rand() % lNumElements;
        long lPageNum = lElementIndex / dwPageSize;

        // Write the ASCII equivalent of the write count
        // to the array.
        //
        BYTE bValueToWrite = '\x30' + bWriteCount;

        printf(
            "About to write %c to array at index %d (page %d).\n",
            bValueToWrite,
            lElementIndex,
            lPageNum
        );

        ReallyBigArray.SetAt(
            lElementIndex,
            bValueToWrite
        );
    }

    // We'll write to the very first and last elements of the
    // array to test our boundary conditions.
    //
    ReallyBigArray.SetAt(lFirstValidIndex, 'A');
    ReallyBigArray.SetAt(lLastValidIndex, 'Z');

    // Try to reach out of bounds on purpose, just to
    // test things out.
    //
    printf(
        "ReallyBigArray[-1] = %c\n",
        ReallyBigArray.GetAt(-1)
    );

    ReallyBigArray.SetAt(lLastValidIndex + 1, '!');

    // Now, to see if our writes have been working,
    // read the first and last elements of the array.
    //
    printf(
        "ReallyBigArray[%d] = %c\n"
        "ReallyBigArray[%d] = %c\n",
        lFirstValidIndex, ReallyBigArray.GetAt(lFirstValidIndex),
        lLastValidIndex, ReallyBigArray.GetAt(lLastValidIndex)
    );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -