- 论坛徽章:
- 0
|
我现在在主程序创建连接字段,传入线程处理函数,但仅查询就会出错。代码如下(DBHandlerImpl是一个封装数据库操作的类,太长,就不贴在这里了,是.pc文件,通过proc预编译为cpp文件):
#include "DBHandlerImpl.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <pthread.h>
using namespace std;
void* threadHandle0(void* pArg);
void* threadHandle1(void* pArg);
int main(void)
{
DBHandlerImpl* pDBHandler[2];
pthread_t id[2];
pDBHandler[0] = new DBHandlerImpl();
pDBHandler[0]->connect("avs/ora142@37");
pDBHandler[1] = new DBHandlerImpl();
pDBHandler[1]->connect("avs/ora142@37");
pthread_create(&id[0], NULL, threadHandle0,(void*)pDBHandler[0]);
pthread_create(&id[1], NULL, threadHandle1,(void*)pDBHandler[1]);
pthread_join(id[0], NULL);
pthread_join(id[1], NULL);
pDBHandler[0]->close();
delete pDBHandler[0];
pDBHandler[1]->close();
delete pDBHandler[1];
return 0;
}
void* threadHandle0(void* pArg)
{
cout<<"in the thread 0"<<endl;
DBHandlerImpl* pDBHandlerImpl = (DBHandlerImpl*)pArg;
string strSql = "select * from A1";
Recordset rs;
int nRet = pDBHandlerImpl->execute(strSql.c_str(), &rs, SQL_SELECT, true);
cout<<"查询结果:"<<nRet<<endl;
}
void* threadHandle1(void* pArg)
{
cout<<"in the thread 1"<<endl;
DBHandlerImpl* pDBHandlerImpl = (DBHandlerImpl*)pArg;
string strSql = "select * from A2";
Recordset rs;
int nRet = pDBHandlerImpl->select(strSql.c_str(), &rs);
cout<<"查询结果:"<<nRet<<endl;
}
执行结果:
in the thread 0
in the thread 1
Segmentation Fault(coredump)
是不是不能有同一个进程中的两个线程同时操作数据库呀?
[ 本帖最后由 starsea3 于 2006-7-26 14:22 编辑 ] |
|