Tcl和C线程的动态共享内存空间

时间:2012-09-20 12:36:34

标签: c multithreading tcl

我希望在Tcl和C线程之间有一个动态共享内存空间。

在运行xxx_Init (Tcl_Interp *interp) {...}过程时会分配该线程共享内存空间的大小,但我想根据Tcl变量为其分配空间,以便最大限度地利用内存。

有可能吗?如果是的话,怎么做?

编辑:详细的代码提供了我想要做的和我的问题。

指向包含共享数据的结构的指针将作为客户端数据与名为testCmd的过程共享,该过程在两个Tcl的变量之后分配内存空间大小。它还与新创建的线程共享为客户端数据。 C扩展在下面详细说明但不起作用,因为线程之间没有共享内存空间,应该在xxx_Init (Tcl_Interp *interp) {...}过程中定义。但是如果我这样做,我就无法获得Tcl的变量来指定要分配的内存空间大小。

    #include <tcl.h>

typedef struct dataHandle_ {
    char *data ;
    long p1 ;
    long p2 ;
} dataHandle_T ;

// Thread function
// Test if we can write memory space allocated by 'testCmd0' function
static void startRoutine (ClientData clientData) {
    dataHandle_T *dH = (dataHandle_T *) clientData;
    //test
    FILE *file;
    file=fopen("testFile.txt", "w");
    while (1) { 
        int mul=dH->p1*dH->p2;
        if (mul<10) {
            dH->data="A";
        } else {
            dH->data="Large str";
        }
        fprintf(file, "Memory size is %d, word is %s ", mul, dH->data);
    }
    fclose(file);
}


// Test command
// Allocating a new memory space for thread shared memory, depending on 2 Tcl variables values
int testCmd(
    ClientData data,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST objv[])
{
    dataHandle_T *dH = (dataHandle_T *)data ;

    // Check the number of arguments
    if (objc != 3) {
        Tcl_WrongNumArgs(interp, 1, objv, "arg arg");
        return TCL_ERROR;
    }

    long p1, p2, result ;

    if ( Tcl_GetLongFromObj(interp, objv[1], &p1) != TCL_OK)
        return TCL_ERROR ;

    if ( Tcl_GetLongFromObj(interp, objv[2], &p2)  != TCL_OK)
        return TCL_ERROR ;

    // Is a re-allocation needed?
    if (dH->p1 != p1 || dH->p2 != p2) {
        if (dH->data != NULL)
            Tcl_Free(dH->data) ;
        dH->data = Tcl_Alloc(p1 * p2 * sizeof(char)) ;// Or whatever allocation you need
    }

    return TCL_OK ;

}

// Create thread launching startRoutine procedure with a dataHandle_T as argument
createThread_Cmd(
    ClientData cdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{

    // Contain the ID of the newly created thread
    Tcl_ThreadId id;    
    // Thread argument
    ClientData limitData;

    // Transfering global var argument to the created thread
    limitData=cdata;

    // Thread creation
    id=0;
    Tcl_CreateThread(&id, startRoutine, limitData, TCL_THREAD_STACK_DEFAULT, TCL_THREAD_NOFLAGS);

    // Return thread ID to tcl prog to allow thread management
    Tcl_SetObjResult(interp, Tcl_NewIntObj((int) id));
    return TCL_OK;  
}


// Note the casing on the _Init function name
int DLLEXPORT

Test_Init(Tcl_Interp *interp)
{
    // Link with the stubs library to make the extension as portable as possible
    if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
        return TCL_ERROR;
    }

    // Declare which package and version is provided by this C code
    if ( Tcl_PkgProvide(interp, "BasicTclExtn", "1.0") != TCL_OK ) {
        return TCL_ERROR ;
    }

    // Allocate the storage for the ClientData
    dataHandle_T *hD = (dataHandle_T *)Tcl_Alloc(sizeof(dataHandle_T));

    // Initialise the new structure
    hD->data = NULL ;
    hD->p1 = -1 ;
    hD->p2 = -1 ;

    // Create a command
    Tcl_CreateObjCommand(interp, "test", testCmd, (ClientData)hD, (Tcl_CmdDeleteProc *)NULL);
    Tcl_CreateObjCommand(interp, "createThread", createThread_Cmd, (ClientData)hD, NULL);
    return TCL_OK ;
}

执行以下Tcl代码时:

load [file join [pwd] libtest[info sharedlibextension]]
test 1 2

set threadId [createThread]
puts "Created thread $threadId"
after 500
# ==Produce an error==
test 4 3
after 500
test 10 20
# =====
exit 1

代码的输出给出(如果产生错误的部分是否被注释):

$ tclsh test.tcl
Created thread -1227109568
$ tclsh test.tcl
Created thread -1227019456
alloc: invalid block: 0x431abb: 69 2e
Aborted (core dumped)

0 个答案:

没有答案