使用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.
该标识描述如下:
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;
}