免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2159 | 回复: 0
打印 上一主题 下一主题

测试在多线程下操作sqlite内存数据库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-22 20:37 |只看该作者 |倒序浏览
硬件平台: Linux on vmware6
操作系统:Trustix Linux3
SQLITE: sqlite-3.5.7
以下摘自:http://www.sqlite.org/c3ref/open.html
int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);
If the filename is ":memory:", then an private
in-memory database is created for the connection.  This in-memory
database will vanish when the database connection is closed.  Future
version of SQLite might make use of additional special filenames
that begin with the ":" character.  It is recommended that
when a database filename really does begin with
":" that you prefix the filename with a pathname like "./" to
avoid ambiguity.
我在主线程中使用
int sqlitehandle = sqlite3_open(":memory:", &pdb);
创建内存数据库,并创建表:
int rs = sqlite3_exec(pdb, "CREATE TABLE testtbl(id int NOT NULL, name varchar(100))", 0, 0,0);
然后创建两个新的线程,将pdb传入线程中,主线程进入睡眠。
在两个线程中,均执行:
  while(1)  {    int rs = sqlite3_exec(pdb, "INSERT INTO testtbl values(25,'aaaaaaaaaaaa')", 0,0,0);    printf("rs = %d\n", rs);    if(rs != SQLITE_OK)    {        printf("err rs=%d\n", rs);        break;    }    usleep(10); }
测试结果显示是可以两个线程操作同一个句柄的!!!
并且在程序运行时,我用lsof命令查看该进程的句柄状态,确实没有任何磁盘的操作!!!
但有个问题不知道是否是由于我是虚拟机引起,即该程序在执行(约1分钟后),两个线程像是进入死锁状态,再没有任何动作,没有退出,也没有显示insert失败!这暂时不理解。


       
        文件:sqlite-3.5.7.tar.gz
        大小:2506KB
        下载:
下载
       


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/870/showart_504264.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP