Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to share an 'chan' between 2 threads

70 views
Skip to first unread message

Helmut Giese

unread,
Apr 23, 2011, 8:34:48 AM4/23/11
to
Hello out there,
from the 'chan' man page
---
Note that this permits the creation of a channel whose two endpoints
live in two different threads, providing a stream-oriented bridge
between these threads. In other words, we can provide a way for
regular stream communication between threads instead of having to send
commands.
---
I don't see how this is to be accomplished: If I do a
[thread::transfer ...] I lose access to the channel.

Any example will be greatly appreciated.
Best regards
Helmut Giese

Donal K. Fellows

unread,
Apr 23, 2011, 12:35:33 PM4/23/11
to
On Apr 23, 1:34 pm, Helmut Giese <hgi...@ratiosoft.com> wrote:
> I don't see how this is to be accomplished: If I do a
> [thread::transfer ...] I lose access to the channel.

Tell the other thread to [thread::attach] to the channel.

Donal.

Tcl Bliss

unread,
Apr 23, 2011, 3:05:34 PM4/23/11
to

If I understood correctly, you need bidirectional communication
between two threads. I that case:

Take a look at the "fifo2" command of "Memchan" package:
http://www.flightlab.com/~joe/gutter/doc/memchan-2.2.1/fifo2.html
or
"fifo2" command of "tcl::chan::fifo2" package:
http://docs.activestate.com/activetcl/8.5/tcllib/virtchannel_base/fifo2.html

Helmut Giese

unread,
Apr 23, 2011, 4:43:45 PM4/23/11
to

But before this I have to [thread::detach] from it in the thread where
it got created.

Oh, wait - I got it.
Thanks and best regards
Helmut Giese

Helmut Giese

unread,
Apr 23, 2011, 4:49:47 PM4/23/11
to
Hi,

actually I was working with a fifo2-type channel, but I needed the
link above "to see the light": When it said
---
One particular application for this is communication between threads,
with one half of the pair moved to the thread to talk to.
---
I eventually got it. I do now
lassign [fifo2] slaveIn masterOut
lassign [fifo2] masterIn slaveOut

thread::detach $slaveIn
thread::detach $slaveOut
in the main thread and later a
thread::attach $slaveIn
thread::attach $slaveOut
in the slave thread and everything works now.

Many thanks and best regards
Helmut Giese

Tcl Bliss

unread,
Apr 23, 2011, 6:00:06 PM4/23/11
to
On Apr 23, 1:49 pm, Helmut Giese <hgi...@ratiosoft.com> wrote:
> Hi,
>
>
>
>
>
>
>
>
>
> >On Apr 23, 5:34 am, Helmut Giese <hgi...@ratiosoft.com> wrote:
> >> Hello out there,
> >> from the 'chan' man page
> >>         ---
> >> Note that this permits the creation of a channel whose two endpoints
> >> live in two different threads, providing a stream-oriented bridge
> >> between these threads. In other words, we can provide a way for
> >> regular stream communication between threads instead of having to send
> >> commands.
> >>         ---
> >> I don't see how this is to be accomplished: If I do a
> >> [thread::transfer ...] I lose access to the channel.
>
> >> Any example will be greatly appreciated.
> >> Best regards
> >> Helmut Giese
>
> >If I understood correctly, you need bidirectional communication
> >between two threads. I that case:
>
> >Take a look at the "fifo2" command of "Memchan" package:
> >http://www.flightlab.com/~joe/gutter/doc/memchan-2.2.1/fifo2.html
> >   or
> >"fifo2" command of "tcl::chan::fifo2" package:
> >http://docs.activestate.com/activetcl/8.5/tcllib/virtchannel_base/fif...

>
> actually I was working with a fifo2-type channel, but I needed the
> link above "to see the light": When it said
>         ---
> One particular application for this is communication between threads,
> with one half of the pair moved to the thread to talk to.
>         ---
> I eventually got it. I do now
>         lassign [fifo2] slaveIn masterOut
>         lassign [fifo2] masterIn slaveOut
>
>         thread::detach $slaveIn
>         thread::detach $slaveOut
> in the main thread and later a
>         thread::attach $slaveIn
>         thread::attach $slaveOut
> in the slave thread and everything works now.
>
> Many thanks and best regards
> Helmut Giese

Note that fifo channels are bidirectional (full duplex) therefore you
only need one pair of channels.

Donal K. Fellows

unread,
Apr 24, 2011, 1:23:52 AM4/24/11
to
On Apr 23, 9:43 pm, Helmut Giese <hgi...@ratiosoft.com> wrote:

> "Donal K. Fellows" wrote:
> > Tell the other thread to [thread::attach] to the channel.
>
> But before this I have to [thread::detach] from it in the thread where
> it got created.

Well, [thread::transfer] is rather like a [thread::detach] and a
[thread::attach] (in the appropriate threads). In fact, it's so much
like that that I'd call [thread::transfer] a convenience command.

Donal.

Andreas Kupries

unread,
Apr 25, 2011, 11:18:58 PM4/25/11
to
Helmut Giese <hgi...@ratiosoft.com> writes:

> Hello out there,
> from the 'chan' man page
> ---
> Note that this permits the creation of a channel whose two endpoints

Two endpoints. I.e. two channels, which are coupled internally. You
transfer one of the two channels to the other thread. Now data written
by one thread can be read from the other, with the channels internally
doing the transfer across the thread boundaries.

The bad news: This is all stuff you have to write on your own. In the
Tcl handler for the virtual channels.

The good news: This has already been done, and the resulting Tcl code
is in Tcllib. See

http://docs.activestate.com/activetcl/8.5/tcllib/virtchannel_base/fifo2.html

If you want this thing in C, instead via 'chan create', then Memchan
is the package for you, especially its fifo2 command.

--
So long,
Andreas Kupries <akup...@shaw.ca>
<http://www.purl.org/NET/akupries/>
Developer @ <http://www.activestate.com/>
-------------------------------------------------------------------------------

Helmut Giese

unread,
Apr 26, 2011, 7:48:41 AM4/26/11
to

>Note that fifo channels are bidirectional (full duplex) therefore you
>only need one pair of channels.

Well, that's even better. Many thanks.

Helmut Giese

unread,
Apr 26, 2011, 7:50:44 AM4/26/11
to
Hi Andreas,

>http://docs.activestate.com/activetcl/8.5/tcllib/virtchannel_base/fifo2.html
>
>If you want this thing in C, instead via 'chan create', then Memchan
>is the package for you, especially its fifo2 command.

yes, TclBliss pointed me to the fifo2 command.
Thanks and best regards
Helmut Giese

0 new messages