Reputation: 355
I have a Tcl main program and I want to create a C thread from it. I then would need to share information between the two threads: C thread's process frequently updated inputs/outputs. I see two possible solutions to my problem: (1) port Tcl's Thread Shared Variable to C, but I didn't see any information about it in the TCL-C API. (2) Create Tcl-C linked Variables and use it as arguments during the C thread creation. The latter idea doesn't seem to work. Here is the C code:
#include <tcl.h>
/*
startRoutine
*/
static void startRoutine (ClientData clientData) {
int *Var;
Var= (int *) clientData;
int locA=0;
int j;
int k;
while (1) {
if (locA=!*Var) {
// Modify Tcl-C shared variable
locA=2 * *Var;
*Var=locA;
for (j=0; j<100; j++){}
} else {
for (k=0; k<100; k++){}
}
}
}
static int
createThreadC_Cmd(
ClientData cdata,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
// Contains 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);
// Wait thread process, before returning to TCL prog
int i;
int aa;
for (i=0 ; i<10000000 ; i++){
aa=i;
}
// Return thread ID to tcl prog to allow mutex use
Tcl_SetObjResult(interp, Tcl_NewIntObj((int) id));
return TCL_OK;
}
int DLLEXPORT
Behavcextension_Init(Tcl_Interp *interp)
{
if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
return TCL_ERROR;
}
// Create global Var
int *sharedPtr;
int linkedVar=0;
sharedPtr=&linkedVar;
Tcl_LinkVar(interp, "linkedVar", (char *) sharedPtr, TCL_LINK_INT);
Tcl_CreateObjCommand(interp,
"createThreadC", createThreadC_Cmd, sharedPtr, NULL);
return TCL_OK;
}
Here is the Tcl code:
# linkedVar initial value in Tcl, will be overwritten by C Tcl_LinkVar() function
set linkedVar 98
puts "linkedVar: $linkedVar"
# Thread creation
#------------------
load [file join [pwd] libBehavCextension[info sharedlibextension]]
set threadId [createThreadC]
puts "Created thread $threadId, waiting"
# When Tcl_LinkVar() is called, initiate linkedVar at 2
puts "linkedVar: $linkedVar"
# Function inside thread should modify linkedVar into linkedVar*2
set linkedVar 98
after 5000
puts "linkedVar: $linkedVar"
The terminal output is here:
Main thread ID: tid0xb779b6c0
linkedVar: 98
Created thread -1227252928, waiting
linkedVar: 2
linkedVar: 98
The last result should be 2*98=196. LinkVar creation between Tcl and C is Ok (we get 2 after link creation), but passing LinkVar to the Thread is KO. Any solution or explanations about why it doesn't work/what to do to solve it are welcome!
Upvotes: 1
Views: 863
Reputation: 137567
The problem remains the same as in the other question. You're allocating the storage for the variable on the C side on the C stack in a function that terminates shortly afterwards. It's Undefined Behavior to refer to that variable (which is linkedVar
in Behavcextension_Init
) after the termination of the function (Behavcextension_Init
). What actually happens is that the actual storage is used for some other function call (doing who knows what) and so the value contained is arbitrary, and changing it can lead to “exciting” behavior.
You're looking to have a variable that exists after Behavcextension_Init
finishes, so it must not be allocated in the stack of that function. The simplest method is this:
int DLLEXPORT
Behavcextension_Init(Tcl_Interp *interp)
{
int *sharedPtr;
if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
return TCL_ERROR;
}
sharedPtr = (int *) Tcl_Alloc(sizeof(int)); // Allocate
*sharedPtr = 0; // Initialize
Tcl_LinkVar(interp, "linkedVar", (char *) sharedPtr, TCL_LINK_INT);
Tcl_CreateObjCommand(interp,
"createThreadC", createThreadC_Cmd, sharedPtr, NULL);
return TCL_OK;
}
Tcl_Free
for that Tcl_Alloc
. For memory allocated once per process, that's not much of a problem. After all, it's only a few bytes and the OS will reclaim it at exit.Tcl_LinkVar
— it knows nothing about mutex-protected memory — but Tcl_LinkVar
is just a wrapper round Tcl_TraceVar
that provides a callback that does the coupling between Tcl's variable (see Tcl_GetVar
and Tcl_SetVar
) and the C variable; writing your own that knows how to do mutex-protection handling as well is not hard. (If you're interested, get the source to Tcl_LinkVar
and adapt it yourself; it doesn't use any private API calls.)Upvotes: 1