【发布时间】:2014-06-18 18:45:54
【问题描述】:
我有这个对数据库的函数调用,我正在使用 C 和 tcl/tk,所以这个函数的 tcl 调用在 C 中执行,但是当这种情况发生时,tcl 中的系统在处理查询时挂起调用和它填充向量的时间,我希望 tcl 应用程序继续工作并将这个过程放在一个单独的线程中。这就是我现在正在尝试的。
处理数据库调用并填充向量的 C 函数:
static int getEpochPrototype(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]){
Tcl_Obj *result;
char sampleid[15];
char tclxVec[15];
char tclyVec[15];
int length;
int numResults;
int i;
Blt_Vector *xCVec, *yCVec;
if (objc != 4) {
Tcl_WrongNumArgs(interp, 3, objv, "number of argument error");
return TCL_ERROR;
}
strcpy(sampleid,Tcl_GetStringFromObj(objv[1], &length));
strcpy(tclxVec,Tcl_GetStringFromObj(objv[2], &length));
strcpy(tclyVec,Tcl_GetStringFromObj(objv[3], &length));
if (Blt_VectorExists(interp, tclxVec) != TCL_OK || Blt_VectorExists(interp, tclyVec) != TCL_OK) {
if ((Blt_GetVector(interp, tclxVec, &xCVec) != TCL_OK) || (Blt_GetVector(interp, tclyVec, &yCVec) != TCL_OK)) {
return TCL_ERROR;
}
} else {
printf("Vector not found \n");
return TCL_ERROR;
}
char command[256];
PQclear(res);
strcpy(command, "select extract ('epoch' from \"Timestamp\"), \"BioAccumulated\" from \"Results\" where \"SampleID\" = '");
strcat(command, sampleid);
strcat(command, "' order by \"Timestamp\" asc");
res = PQexec(conn,command);
numResults = PQntuples(res);
double x[numResults], y[numResults];
for (i = 0; i < numResults; i++)
{
x[i] = strtod(PQgetvalue(res,i,0), NULL);
y[i] = strtod(PQgetvalue(res,i,1), NULL);
}
/* Put the data into BLT vectors */
if ((Blt_ResetVector(xCVec, x, numResults, numResults, TCL_VOLATILE) != TCL_OK) || (Blt_ResetVector(yCVec, y, numResults, numResults, TCL_VOLATILE) != TCL_OK)) {
return TCL_ERROR;
}
return TCL_OK;
}
TCL 函数试图将 blt 向量发送到 c 线程:
blt::vector create xVec
blt::vector create yVec
tsv::set graph xtemp_shared xVec
tsv::set graph yBio_shared yVec
thread::create {
load ./samples.so
connectDB2
#puts "[tsv::get details sampleid_shared]"
getEpochPrototype [tsv::get details sampleid_shared] [tsv::get graph xtemp_shared] [tsv::get graph yBio_shared]
}
当我尝试这个时,我得到“找不到向量”,这是我在找不到向量或出现错误时输入的打印语句,有没有其他方法可以做到这一点,或者我错过了什么?
【问题讨论】:
-
tsv::set是 C 中的语法错误。您的意思是 C++ 吗?
标签: c multithreading tcl