Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tk app in thread

Status
Not open for further replies.

meaculpa

MIS
Jun 13, 2003
2
NL
I have a very simple application. The printf in myProc produces no results if i run it on Linux. On Solaris 7 it prints the message "this isn't a Tk application".
Why?

Compilation and linking:
gcc -o mytk.run mytk.c -L/usr/openwin/lib -ltcl -ltk -lX11 -ldl -lm -lpthread

Tcl8.3.3\Tk8.3.3
gcc3.0.3

Thanks in advance.

#include <stdio.h>
#include <tcl.h>
#include <tk.h>
#include <pthread.h>

void *myProc(void *interp)
{
Tcl_Interp *i = (Tcl_Interp *)interp;
Tk_MainWindow(i);
printf(&quot;result: %s\n&quot;, i->result);
}

int main(int argc, char** argv)
{
Tk_Window mainWindow;
Tcl_Interp *interp;
interp = Tcl_CreateInterp();

Tcl_Init(interp);
Tk_Init(interp);

mainWindow = Tk_MainWindow(interp);

Tcl_Eval(interp, &quot;button .b -text \&quot;Hi\&quot; -command exit&quot;);
Tcl_Eval(interp, &quot;pack .b&quot;);

pthread_t *thr = (pthread_t*) malloc(sizeof(pthread_t));
pthread_create(thr, 0, &myProc, (void *) interp);

Tk_MainLoop();
exit(1);
}
 
Well, I see two problems with your code. I'm not sure if either of them is responsible for the error you're encountering, but you won't get very far without addressing these issues.

First, you need to call Tcl_FindExecutable() before creating your interpreter. See my comments in thread287-293264, &quot;HOWTO init Tcl on Windows?&quot; for an explanation.

But you have a more serious problem in the structure of your multi-threaded application. To quote from the &quot;Tcl Threading Model&quot; whitepaper:

&quot;Tcl lets you have one or more Tcl interpreters (e.g., created with Tcl_CreateInterp()) in each operating system thread. However, each interpreter is tightly bound to its OS thread and errors will occur if you let more than one thread call into the same interpreter (e.g., with Tcl_Eval()).&quot; (Emphasis mine.)

In your application, you create your Tcl interpreter in your main thread, and then pass a pointer to the interpreter to your worker thread, which then tries to manipulate the interpreter. This isn't allowed in Tcl's threading model. For more information on Tcl's threading model, read the whitepaper at You can also read some comments and suggestions on the Tcl'ers Wiki, I'd suggest starting with &quot;Tcl and threads,&quot; - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
I appreciate deeply your help on this.
Now I'm wandering why this code works on Linux? As i understand tcl has the same threading model both on Solaris and on Linux.
Besides, that was just the example of the problem. The real problem is in more complex application. In that application Tcl works fine(e.g Tcl_Eval function) even on Solaris if no Tk functions being used. Maybe you know why?
Thanks again.
 
Well, I've tried to avoid messing around with multi-threaded Tcl applications at the C level. (Multi-threaded coding is fraught with danger -- unforeseen race conditions, accidental concurrent access to resources, unwitting use of non-thread-safe libraries, etc. -- and so I count myself fortunate that Tcl's event-driven model has served virtually all of my needs.) So, It's difficult for me to speak authoritatively as to what might be going on. Except...

I know that Tcl itself is thread-safe. However, Tk is not completely thread-safe. Not too surprising, as the graphics libraries upon which it relies are not thread-safe on many platforms. Therefore, when you weren't using Tcl's threading model but stuck to the Tcl APIs only, Tcl handled things fairly gracefully. However, once you started adding Tk to the mix, you began to run afoul of the non-thread-safe areas of Tk.

The model recommended for multi-threaded GUI applications holds for multi-threaded Tcl programming as well: Only one thread should &quot;own&quot; the GUI. All other threads that want to update the GUI should send requests to the GUI thread. Implementing this with Tcl, this means that you'll have multiple interpreters, each one in its own thread, but only one of those interpreters will initialize Tk and use the Tk APIs. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top