Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #27799

Re: avcall, callback, trampoline, vacall, etc

From "Pascal J. Bourguignon" <pjb@informatimago.com>
Newsgroups comp.lang.c, comp.lang.c++, comp.unix.programmer, comp.lang.javascript, comp.lang.lisp
Subject Re: avcall, callback, trampoline, vacall, etc
Date 2012-10-28 18:56 +0100
Organization Informatimago
Message-ID <87r4oiiiq0.fsf@informatimago.com> (permalink)
References <dfa26195-ed53-41e7-afb6-6f59d556a154@3g2000yqn.googlegroups.com> <1ba9va6kmw.fsf@new-snowball.wb.pfeifferfamily.net>

Cross-posted to 5 groups.

Show all headers | View raw


Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:

> For callback-based programming, forget all that.  Flush it.
>
> There are areas where a completely different paradigm is used.  A good example
> of one of these areas is writing a GUI interface.  The big thing about a
> GUI interface is that you don't have control of the order in which input
> arrives, and you need to do something about whatever the crazy user
> does.  In this world, somebody else writes something called an "event
> loop".  The event loop just sits out there waiting for input (maybe a
> button click, maybe information that you need to redraw your window,
> maybe a timeout, maybe something else), and when the input happens the
> event loop code calls a function you wrote to handle that particular
> input.  Your function is called a 'callback'.

Yes, but in general, there is still this direct and simple flow of
control: a function calls another function, and so on.

A callback is just a function that is given to some module, to be called
when the module needs to call it.  This is a way to parameterize or
customize that module.

    (defun my-callback (data)
      (format t "Received ~A~%" data))

    (defun receive-loop (data-callback)
      (loop
         :for line = (progn (format *query-io* "> ")
                            (finish-output *query-io*)
                            (read-line *query-io* nil nil))
         :while (and line (plusp (length line)))
         :do (funcall data-callback line)))

    cl-user> (receive-loop 'my-callback)
    > Hello
    Received Hello
    > World
    Received World
    > 
    nil
    cl-user>  

And then you can use the receive-loop module with a different callback,
making it do something else:


    cl-user> (let ((c 0))
               (receive-loop (lambda (data) (incf c (parse-integer data))))
               c)
    > 42
    > 33
    > 101
    > 
    176
    cl-user>  


Now, there's another situation where you really don't know WHEN the
callback is called, it's when the module calling it uses threads, and
calls your callback function at "random" times, possibly in another
thread, or perhaps while handling a unix signal.  In this situation,
there are a lot of technicalities to fulfill for calbacks.


But the in usual case, there's no complexity involved.

-- 
__Pascal Bourguignon__
http://www.informatimago.com

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-25 12:24 -0700
  Re: avcall, callback, trampoline, vacall, etc James Kuyper <jameskuyper@verizon.net> - 2012-10-25 16:04 -0400
    Re: avcall, callback, trampoline, vacall, etc fmassei@gmail.com - 2012-10-25 13:08 -0700
      Re: avcall, callback, trampoline, vacall, etc James Kuyper <jameskuyper@verizon.net> - 2012-10-25 16:12 -0400
        Re: avcall, callback, trampoline, vacall, etc fmassei@gmail.com - 2012-10-25 13:23 -0700
          Re: avcall, callback, trampoline, vacall, etc Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-25 20:01 -0600
    Re: avcall, callback, trampoline, vacall, etc Gene Wirchenko <genew@ocis.net> - 2012-10-25 14:55 -0700
  Re: avcall, callback, trampoline, vacall, etc Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-25 15:46 -0500
    Re: avcall, callback, trampoline, vacall, etc "James K. Lowden" <jklowden@speakeasy.net> - 2012-10-25 18:53 -0400
      Re: avcall, callback, trampoline, vacall, etc Öö Tiib <ootiib@hot.ee> - 2012-10-26 13:18 -0700
        Re: avcall, callback, trampoline, vacall, etc fmassei@gmail.com - 2012-10-26 13:33 -0700
  Re: avcall, callback, trampoline, vacall, etc Cortez <relativeflux@hotmail.co.uk> - 2012-10-25 13:47 -0700
  Re: avcall, callback, trampoline, vacall, etc Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-25 20:18 -0600
    Re: avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-26 10:11 -0700
    Re: avcall, callback, trampoline, vacall, etc Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-27 18:17 +0000
      Re: avcall, callback, trampoline, vacall, etc Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-27 21:35 -0600
        Re: avcall, callback, trampoline, vacall, etc Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-29 12:38 +0000
    Re: avcall, callback, trampoline, vacall, etc "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-10-28 18:56 +0100
    Re: avcall, callback, trampoline, vacall, etc Rivka Miller <rivkaumiller@gmail.com> - 2012-11-05 16:28 -0800
  Re: avcall, callback, trampoline, vacall, etc Les Cargill <lcargill99@comcast.com> - 2012-10-26 07:27 -0500
    Re: avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-26 10:16 -0700
      Re: avcall, callback, trampoline, vacall, etc Les Cargill <lcargill99@comcast.com> - 2012-10-26 12:42 -0500
      Re: avcall, callback, trampoline, vacall, etc James Kuyper <jameskuyper@verizon.net> - 2012-10-26 14:08 -0400

csiph-web