No registered users in community xowiki
in last 10 minutes
in last 10 minutes
Re: [Xotcl] XOTcl and Thread
From: <mail_at_xdobry.de>
Date: Fri, 11 Feb 2011 08:37:42 +0100 (CET)
Hi Krzysztof!
I have run also into this problem (trap).
Threads in Tcl are different than in Java or C#.
The tcl threads are more like new independent processes (at Tcl level).
New Thread in Tcl is just new Interpreter. Interpreter is environment to run Tcl
code. (see command interp)
Every interpreter/thread has all internal structures (Tcl methods and verialbes)
independent to another interpreter.
You can not share XOTcl Objects between interpreters because object name is just
string reference to internal structure of object.
Serializing object is probably not the solution for this what you need.
You can share only values but not references.
The solution would be create in each thread separate bundle of XOTcl objects
that can communicate with each other by sharing values (just strings)
My personal experience is not to use threads at all.
You can use internal event handler of Tcl to simulate parallel processing.
For example for GUI to program not freezing GUI or aborts buttons.
Use commands:
after
update
vwait (Works also with XOTcl variables)
I have programmed spell checker for editor which runs in "update" loop and for
user it seems to be completely parallel. Using tcl threads for it will be
overkill.
Also programming IO operations (for example Tcl Server) with events is better
then using threads.
Tcl has good support for asynchronous IO operations.
Tcl has very good event handling loop that is nearly the same that old OS
uses (Windows 3.1) to simulate parallel processing.
See tcl wiki for some examples.
Artur
Krzysztof Frukacz <frukacz.krzysztof_at_gmail.com> hat am 10. Februar 2011 um 14:27
geschrieben:
> Hello again,
>
> I am trying to add multi-threaded support for my application. I now face
> a problem witch combining XOTCl and Thread. The problem is that
> ::thread::create creates a new instance of interpreter which has own
> variables. I can have variables shared between threads using ::tsv
> functions. So here is how it looks:
>
> 1. Thread #1 creates object myObject and stores in a shared variable:
> ::tsv::set array1 key1 [MyClass myObject]
>
> 2. Thread #2 gets the shared variable and tries to call a proc defined
> for MyClass:
> set localVar [::tsv::get array1 key1]
> $localVar doSomething
>
> At this point second thread will throw en exception saying:
> Unknown command 'myObject'
>
> What would be the proper way to "register" the object myObject (which
> already exists) in another thread?
>
> --
> Krzysztof
> _______________________________________________
> Xotcl mailing list
> Xotcl_at_alice.wu-wien.ac.at
> http://alice.wu-wien.ac.at/mailman/listinfo/xotcl
Date: Fri, 11 Feb 2011 08:37:42 +0100 (CET)
Hi Krzysztof!
I have run also into this problem (trap).
Threads in Tcl are different than in Java or C#.
The tcl threads are more like new independent processes (at Tcl level).
New Thread in Tcl is just new Interpreter. Interpreter is environment to run Tcl
code. (see command interp)
Every interpreter/thread has all internal structures (Tcl methods and verialbes)
independent to another interpreter.
You can not share XOTcl Objects between interpreters because object name is just
string reference to internal structure of object.
Serializing object is probably not the solution for this what you need.
You can share only values but not references.
The solution would be create in each thread separate bundle of XOTcl objects
that can communicate with each other by sharing values (just strings)
My personal experience is not to use threads at all.
You can use internal event handler of Tcl to simulate parallel processing.
For example for GUI to program not freezing GUI or aborts buttons.
Use commands:
after
update
vwait (Works also with XOTcl variables)
I have programmed spell checker for editor which runs in "update" loop and for
user it seems to be completely parallel. Using tcl threads for it will be
overkill.
Also programming IO operations (for example Tcl Server) with events is better
then using threads.
Tcl has good support for asynchronous IO operations.
Tcl has very good event handling loop that is nearly the same that old OS
uses (Windows 3.1) to simulate parallel processing.
See tcl wiki for some examples.
Artur
Krzysztof Frukacz <frukacz.krzysztof_at_gmail.com> hat am 10. Februar 2011 um 14:27
geschrieben:
> Hello again,
>
> I am trying to add multi-threaded support for my application. I now face
> a problem witch combining XOTCl and Thread. The problem is that
> ::thread::create creates a new instance of interpreter which has own
> variables. I can have variables shared between threads using ::tsv
> functions. So here is how it looks:
>
> 1. Thread #1 creates object myObject and stores in a shared variable:
> ::tsv::set array1 key1 [MyClass myObject]
>
> 2. Thread #2 gets the shared variable and tries to call a proc defined
> for MyClass:
> set localVar [::tsv::get array1 key1]
> $localVar doSomething
>
> At this point second thread will throw en exception saying:
> Unknown command 'myObject'
>
> What would be the proper way to "register" the object myObject (which
> already exists) in another thread?
>
> --
> Krzysztof
> _______________________________________________
> Xotcl mailing list
> Xotcl_at_alice.wu-wien.ac.at
> http://alice.wu-wien.ac.at/mailman/listinfo/xotcl