bdb单进程多线程下使用

使用DbEnv->open打开环境的时候,使用DB_PRIVATE标识,此时不会创建区域(region)文件(如__db.001等)
该标识描述如下:
DB_PRIVATE
    Specify that the environment will only be accessed by a single process (although that process may be multithreaded). This flag has two effects on the Berkeley DB environment. First, all underlying data structures are allocated from per-process memory instead of from shared memory that is potentially accessible to more than a single process. Second, mutexes are only configured to work between threads.

使用示例:

#include "db_cxx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    u_int32_t env_flags = DB_CREATE|DB_INIT_MPOOL|DB_PRIVATE|DB_THREAD;
    u_int32_t db_flags  = DB_CREATE;
    string envHome(".");
    string dbName("mydb.db");

    DbEnv myEnv(0);
    Db* myDb = NULL;

    try
    {
        myEnv.open(envHome.c_str(), env_flags, 0);
        myDb = new Db(&myEnv, DB_CXX_NO_EXCEPTIONS);
        if (myDb != NULL)
        {
            myDb->open(NULL, dbName.c_str(), NULL, DB_BTREE, db_flags, 0);
            myDb->close(0);
        }

        //关闭DB环境
        myEnv.close(0);
    }
    catch (DbException &e)
    {
        std::cerr << e.what() << std::endl;
        exit(-1);
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(-1);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值