For Programmers: Free Programming Magazines  


Home > Archive > Tcl > June 2006 > fileevent callback: parallel to main execution?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fileevent callback: parallel to main execution?
susara

2006-06-19, 8:03 am

I think I know the answer to this but the documentation isn't
absolutely clear so let me ask here.

When using open with the pipe to exec a programme, that programme
obviously runs in parallel to the main execution thread. The main
thread can do a fileevents on the returned pipe, setting up a callback
function to be invoked whenever the pipe becomes readable/writeable.

What's not clear to me is whether that callback is executed in parallel
to the main execution context or not. When the callback and the main
context shares global variables; do I need to protect those variables
against concurrent access?

Jonathan Bromley

2006-06-19, 8:03 am

On 19 Jun 2006 06:18:24 -0700, "susara" <judithr@inet.co.za> wrote:

>I think I know the answer to this but the documentation isn't
>absolutely clear so let me ask here.
>
>When using open with the pipe to exec a programme, that programme
>obviously runs in parallel to the main execution thread. The main
>thread can do a fileevents on the returned pipe, setting up a callback
>function to be invoked whenever the pipe becomes readable/writeable.
>
>What's not clear to me is whether that callback is executed in parallel
>to the main execution context or not. When the callback and the main
>context shares global variables; do I need to protect those variables
>against concurrent access?


Others may be able to supply more authoritative answers, but
my understanding is that Tcl event handlers always execute
atomically. If you're using event handlers then presumably your
foreground program (is that what you mean by "main context"?)
is asleep at a [vwait], so it's only event handlers that are
executing anyway. And they execute one at a time,
indivisibly.

I have a nasty feeling that the story would get quite a lot
more complicated if you do a [vwait] within one of your
event handlers. But I never do that, because it hurts my
head too much.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Helmut Giese

2006-06-19, 8:03 am

On 19 Jun 2006 06:18:24 -0700, "susara" <judithr@inet.co.za> wrote:

>I think I know the answer to this but the documentation isn't
>absolutely clear so let me ask here.
>
>When using open with the pipe to exec a programme, that programme
>obviously runs in parallel to the main execution thread. The main
>thread can do a fileevents on the returned pipe, setting up a callback
>function to be invoked whenever the pipe becomes readable/writeable.
>
>What's not clear to me is whether that callback is executed in parallel
>to the main execution context or not. When the callback and the main
>context shares global variables; do I need to protect those variables
>against concurrent access?

Hi,
when you say
using open with the pipe to exec a programme
you have 2 processes running - and processes never share global
variables, they all have their own distinct memory context.
HTH
Helmut Giese
Michael Schlenker

2006-06-19, 7:22 pm

susara schrieb:
> I think I know the answer to this but the documentation isn't
> absolutely clear so let me ask here.
>
> When using open with the pipe to exec a programme, that programme
> obviously runs in parallel to the main execution thread. The main
> thread can do a fileevents on the returned pipe, setting up a callback
> function to be invoked whenever the pipe becomes readable/writeable.
>
> What's not clear to me is whether that callback is executed in parallel
> to the main execution context or not. When the callback and the main
> context shares global variables; do I need to protect those variables
> against concurrent access?
>

No, the event loop is strictly single threaded, so you do not have to
worry about concurrent access, event handlers are strictly serialized.

But this also means your event handlers can starve the rest of the
program if they take too long to handle things, and that you can starve
your event handlers if you don't allow the event loop to process
incoming events.

See:
http://wiki.tcl.tk/1527

Either you go fully event based, e.g. even the main execution context as
you call has to be basically event driven (for example by chaining small
chunks of execution with 'after idle' or 'after 0'). This is the case
for Tk GUIs for example.

See for a good example this page:
http://wiki.tcl.tk/946

You can create deadlock issues if you re-enter the event loop by using
update inside an event handler, usually a bad idea.
http://wiki.tcl.tk/1255

Michael

Gerald W. Lester

2006-06-19, 7:22 pm

susara wrote:
> I think I know the answer to this but the documentation isn't
> absolutely clear so let me ask here.
>
> When using open with the pipe to exec a programme, that programme
> obviously runs in parallel to the main execution thread. The main
> thread can do a fileevents on the returned pipe, setting up a callback
> function to be invoked whenever the pipe becomes readable/writeable.


Threads are not involved here, but rather processes.

> What's not clear to me is whether that callback is executed in parallel
> to the main execution context or not. When the callback and the main
> context shares global variables; do I need to protect those variables
> against concurrent access?


The fileevent callback is invoked at the global level in the process and
thread in which it was declared. It is not an interrupt, but rather an
event that gets processed when you are in the event loop.


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
susara

2006-06-20, 4:36 am

Thanks for responses, I also found this explaination which makes it
quite clear:

http://wiki.tcl.tk/1527

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com