Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:43:25+00:00 2026-06-11T01:43:25+00:00

I am trying to link a Tcl variable to a C variable in order

  • 0

I am trying to link a Tcl variable to a C variable in order to pass the pointer to the latest during C thread creation and have a TCL-C thread shared variable (I don’t think I can use native TCL Thread Shared Variable functions).
I have some difficulties to link both variables.
Here is how I do:

#Tcl code, calling the C function:
set linkedVar 98
puts "linkedVar: $linkedVar"
load [file join [pwd] libCextension[info sharedlibextension]]
set threadId [createThreadC]
puts "Created thread n° $threadId"
puts "linkedVar: $linkedVar"

The createThreadC function creates a C thread, return its ID and tries to create a link with linkedVar.

// C function called by Tcl
static int
createThreadC_Cmd(
    ClientData cdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    int linkedVar=2;
    Tcl_LinkVar(interp, "linkedVar", (char *) &linkedVar, TCL_LINK_INT);
    linkedVar=1;
    ...
    # Thread creation, return Tcl object with thread ID
    ...
    return TCL_OK;  
}

Here is the output:

linkedVar: 98
Created thread n° -1227199680
linkedVar: 35

The linkedVar value changed, as C program had to, but it stores the wrong variable, should be 1 instead of 35.
Is it the (char *) &linkedVar cast which is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:43:26+00:00Added an answer on June 11, 2026 at 1:43 am

    I was going to say the same stuff as Donal, but also wrote a demo. So here it is – basically your linked variable lifetime should match the interpreter lifetime.

    #include <tcl.h>
    
    typedef struct Shared {
        Tcl_Interp *interp;
        int id;
    } Shared;
    
    static int
    UpdateCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
    {
        Shared *sharedPtr = (Shared *)clientData;
        if (objc != 1) {
            Tcl_WrongNumArgs(interp, 1, objv, "");
            return TCL_ERROR;
        }
        ++sharedPtr->id;
        return TCL_OK;
    }
    static void
    DeleteProc(ClientData clientData)
    {
        Shared *sharedPtr = (Shared *)clientData;
        Tcl_UnlinkVar(sharedPtr->interp, "shared_id");
        Tcl_Release(sharedPtr->interp);
        Tcl_Free(clientData);
    }
    
    int DLLEXPORT
    Testlink_Init(Tcl_Interp *interp)
    {
        Shared *sharedPtr;
        if (Tcl_InitStubs(interp, "8.4", 0) == NULL) {
            return TCL_ERROR;
        }
        sharedPtr = (Shared *)Tcl_Alloc(sizeof(Shared));
        sharedPtr->interp = interp;
        sharedPtr->id = 0;
        Tcl_Preserve(sharedPtr->interp);
        Tcl_LinkVar(interp, "shared_id", (char *)&sharedPtr->id, TCL_LINK_INT);
        Tcl_CreateObjCommand(interp, "update_shared", UpdateCmd, sharedPtr, DeleteProc);
        Tcl_PkgProvide(interp, "testlink", "1.0");
        return TCL_OK;
    }
    

    Usage (building too using msvc 6):

    C:\src>cl -nologo -Od -MD -I\opt\tcl\include -DUSE_TCL_STUBS -c tcl_link.c
    tcl_link.c
    
    C:\src>link -dll -debug -out:tcl_link.dll tcl_link.obj \opt\tcl\lib\tclstub85.lib
    Microsoft (R) Incremental Linker Version 6.00.8447
    Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
    
    C:\src>tclsh
    % load tcl_link.dll testlink
    % set shared_id
    0
    % update_shared
    % set shared_id
    1
    

    This shows one way to clean things up by leveraging the command cleanup function.

    You must be very careful if you use multiple threads and also have Tcl interpreters around. A Tcl interp is tied to the thread it was created on. So if you want the Shared structure to be passed among C threads you should loose the interp member. In that case I’d have this structure lifetime being handled by the application. Provided your Shared structure has a lifetime that is longer than any interpreter that has a command for which this is the clientData then everything will be ok and you don’t need to cleanup within the interpreter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to link my program to the shared library. Im using a makefile
I'm trying to link to a shared library with a template class, but it
I am trying to link anchors inside jquery tabbed content. I have gotten it
I am trying to link some Boost .hpp files with Monodevelop, but I don't
Alright, I'm trying to link three different tables (Customers, Orders, and Order Details), in
I am trying to link to an external link using the anchor tag like
I'm trying to link a 2d list of objects to a contentcontrol. I'm getting
i am trying to link a Unity game to a Java server using C#
I'm trying to link two tables in the same database using a third. The
I am trying to link a remote database to a local database using dblink.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.