【问题标题】:how to pass blt vectors in a separate thread如何在单独的线程中传递 blt 向量
【发布时间】: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


【解决方案1】:

Tcl 的threading model 要求您从创建该解释器的线程访问任何特定的Tcl 解释器实例。 (Tcl 的实现广泛使用线程特定的数据。)你可以从另一个线程填充那些 C 数组——那里通常有 C 并行规则——然后使用Tcl_ThreadQueueEvent 告诉另一个线程你有事情要做;您发送的消息实际上是代码+数据。

struct DoneReadingIntoArraysEvent {
    Tcl_Event eventHeader;
    Tcl_Interp *interp;
    double *x, *y;
    int length;
}

struct DoneReadingIntoArraysEvent callback;
callback.eventHeader.proc = &MyCallbackFunc;
callback.interp = theInterpHandleInTheOtherThread;
callback.x = x;
callback.y = y;
callback.length = numberOfValuesInArray;
Tcl_ThreadQueueEvent(mainThreadId, &callback.eventHeader, TCL_QUEUE_TAIL);

现在,在MyCallbackFunc 中,您将Tcl_Event* 参数转换回struct DoneReadingIntoArraysEvent(安全;它是第一个成员!)并检索您需要告诉解释器(以及相关解释器)的值。

为了将值实际导入 Tcl,我强烈考虑使用包含一大堆双 Tcl_Objs 的列表 Tcl_Obj。这是一种在 BLT 首次创建时不可用的机制,它的性能可能比您预期的要好。 或者,如果值的数量很大,而您只需要传递和/或存储它们,请发送一大块二进制文件(binary scan 命令将能够在需要时挑选位)。

相关的 API 是 Tcl_NewListObj、Tcl_NewDoubleObj 和 Tcl_NewByteArrayObj。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多