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

📄 main.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
字号:
/*****************************************************************
 *                                                               *
 * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.     *
 *                                                               *
 *****************************************************************/

/*
 * This sample demonstrates McObject multi-threaded functionality -
 * transaction queue.
 * Multiple OS threads open "read-only" and "read-write" transactions.
 * Some of "read-only" transactions get upgraded to "read-write". Separate
 * "reporter" thread is used to collect thread data and print out
 * reports. The output frequency is regulated by changing REPORTER_DELAY
 * constant (threads.c)
 *
 * No acctual read/writes are performed in this examle; instead the worker threads
 * simply call Sleep() to simulate database activity. The wait time and the number
 * of worker threads could be easily changed by setting the appropriate constatns in
 * threads.c:
 * #define MAXREADERS 10
 * #define MAXWRITERS 4
 * #define WRITER_DELAY   200 // msec
 * #define READER_DELAY   10  // msec
 *
 * This sample could be used as a framework for some real application
 * with miltiple input and output streams.
 *
 */
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include "samples.h"

char dbname[] = "SimpleDb";
static const int FIRST_SEGSZ = 1024 * 8000UL;
#ifndef MCO_PLATFORM_X64
    static const int PAGESIZE = 96;
#else 
    static const int PAGESIZE = 192;
#endif 


const int MAP_ADDRESS = 0x20000000;

void threads(char* name, int inTestMode);

static void _SH_(void)
{

    char text[] = 
    {

        "\nThis sample demonstrates eXtremeDB multi-threaded functionality-\n"
            "the transaction queue.Multiple OS threads open \"read-only\" and \"read-write\"\n"
            "transactions. Some of \"read-only\" transactions get upgraded to \"read-write\".\n"
            "Separate \"reporter\" thread is used to collect thread data and print out\n"
            "reports. No actual database reads or writes are performed in this sample;\n"
            "instead the worker threads simply call Sleep() to simulate database activity.\n"
    };

    char text1[] = 
    {
        "Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"
    };

    printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n\nPress Enter to start", text, MCO_COMP_VER_MAJOR,
           MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM, text1);

    getchar();
}


void showMem(mco_db_h db)
{
    mco_puint freepg, totalpg;

    mco_db_free_pages(db, &freepg);
    mco_db_total_pages(db, &totalpg);

    printf("\n\tMemory Report:\n\ttotal pages=%d (%dK)\n\tfree pages=%d (%dK)\n\tused %dK\n", totalpg, totalpg*
           PAGESIZE / 1024, freepg, freepg* PAGESIZE / 1024, (totalpg - freepg)* PAGESIZE / 1024);
}


/* fatal error handler */
static void errhandler(int n)
{
    printf("\nFatal error: %d", n);
    getchar();
    exit( - 1);
}


int main(int argc, char** argv)
{
    MCO_RET rc;
    mco_db_h db = 0;
    mco_runtime_info_t info;
    char* start_mem;
    int inTestMode = 0;

    if (argc > 1 && strstr(argv[1], "--test"))
    {
        inTestMode = 1;
    }
    _SH_();
    /* check wheather evants are supported by the runtime */
    mco_get_runtime_info(&info);
    if (!info.mco_multithreaded)
    {
        printf("This sample requires multi-threaded runtime\n");
        exit(1);
    }

    if (info.mco_shm_supported)
    {
        start_mem = (char*)MAP_ADDRESS;
    }
    else
    {
        start_mem = (char*)malloc(FIRST_SEGSZ);
        if (!start_mem)
        {
            printf("Couldn't allocated memory\n");
            exit(1);
        }
    };

    rc = mco_runtime_start();
    rc = mco_db_open(dbname, simple_get_dictionary(), start_mem, FIRST_SEGSZ, (uint2)PAGESIZE);

    if (rc)
    {
        printf("\nerror creating database");
        if (!info.mco_shm_supported)
        {
            free(start_mem);
        }
        exit(1);
    }

    /* set application's error handler */
    mco_error_set_handler(&errhandler);

    threads(dbname, inTestMode);

    /* destroy the database */
    mco_db_close(dbname);

    mco_runtime_stop();

    if (!info.mco_shm_supported)
    {
        free(start_mem);
    }

    printf("\npress Enter to finish\n");
    getchar();
    PROG_EXIT(0);
}

⌨️ 快捷键说明

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