2012-03-07 58 views
3

如果TCL配置爲線程的 ,Tcl_PackageInitProc()的幾個實例可以同時執行(在不同的線程中) ?Tcl:擴展加載和線程的交互

由於向後兼容的原因,我認爲 調用的初始化和卸載過程必須是 序列化。

本手冊對行爲保持沉默:在這些例程中是否調用了這些例程 ,或者必須使用擴展編寫器處理同步,在 中進行特定的互斥?

回答

1

Tcl確實不是保證這些函數以序列化的方式被調用;如果你的代碼在意,它必須使用合適的互斥鎖。 Tcl在其C庫中提供了便攜式基元,您可以這樣使用它:

#include <tcl.h> 

// MUCH easier to have this as its own function 
static void OneTimeSetup(void) { 
    static int doneSetup; 
    TCL_DECLARE_MUTEX(myMutex); 

    Tcl_MutexLock(&myMutex); 
    if (!doneSetup) { 
     // Do critical once-only setup here 
     doneSetup = 1; 
    } 
    Tcl_MutexUnlock(&myMutex); 
} 

int My_Init(Tcl_Interp *interp) { 
    // Declare the API version we're using, e.g., for 8.5... 
    if (Tcl_InitStubs(interp, "8.5", 0) == NULL) 
     return TCL_ERROR; 

    // Call out to our setup code 
    OneTimeSetup(); 

    // Install Tcl commands/variables/... here 

    // Ready for action! 
    return TCL_OK; 
}