Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: comp.lang.c,comp.lang.c++,comp.unix.programmer,comp.lang.javascript,comp.lang.lisp Subject: Re: avcall, callback, trampoline, vacall, etc Date: Sun, 28 Oct 2012 18:56:07 +0100 Organization: Informatimago Lines: 69 Message-ID: <87r4oiiiq0.fsf@informatimago.com> References: <1ba9va6kmw.fsf@new-snowball.wb.pfeifferfamily.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net pQCxIIhV8eAfEMry/FkSuAwv2EnRwCqYurS9+wf5BXn36EcIaC+aiqqLvN0PcanPgF Cancel-Lock: sha1:N2RmMDMwMjJkYzk1MWFmMmU0MTI0YmNjNjA4OWE0YTkzZDRlNjllMQ== sha1:dRrZeAL+LpZsgWni1nfhqcTBL2U= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (darwin) Xref: csiph.com comp.lang.c:27799 comp.lang.c++:19315 comp.unix.programmer:3680 comp.lang.javascript:16928 comp.lang.lisp:11798 Joe Pfeiffer 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