473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Callbacks and events

I read a lot of discussion about callback functions as used in C (as in
qsort for example). I'm comfortable with this as it is really a
function pointer. What I'm not totally clear about is how (or if) a
callback differs from an event such as GUI's use. One of the things I
found about events, at least in a VB6 project I did, is that an event
can be broadcast to all objects and they can use it if desired (code
only and GUI objects). Each object has to have a receive function for
that event though.

The only way I can see to do event broadcasting with a callback is to
have a special callback that in turn calls a list of receiver functions
that would be in each module. For example, a timer that broadcasts a
time tick every second would call a function (for example)
Timer_1min(void) that in turn would call functions in all of the modules
that need to know the time. I've also thought about a message que, but
that is begining to sound like too musch work. Is there a better way?

Thanks for any helpful information.

Dave,

Nov 14 '05 #1
2 3274
Dave Boland wrote:
I read a lot of discussion about callback functions as used in C (as in
qsort for example). I'm comfortable with this as it is really a
function pointer. What I'm not totally clear about is how (or if) a
callback differs from an event such as GUI's use. One of the things I
found about events, at least in a VB6 project I did, is that an event
can be broadcast to all objects and they can use it if desired (code
only and GUI objects). Each object has to have a receive function for
that event though.

The only way I can see to do event broadcasting with a callback is to
have a special callback that in turn calls a list of receiver functions
that would be in each module. For example, a timer that broadcasts a
time tick every second would call a function (for example)
Timer_1min(void) that in turn would call functions in all of the modules
that need to know the time. I've also thought about a message que, but
that is begining to sound like too musch work. Is there a better way?


A "callback function" is simply a function. Some piece
of code calls it, it executes, and it returns to the caller.
No mysteries, nothing special about it.

When we say that one function is a callback and another
isn't, we're really not talking about the functions but about
how they happen to get called. When we write a call to an
"ordinary" function we know as we're typing in the code just
which function we want to call, and we write it's name: printf()
or sqrt() or whatever. When we write a call to a callback
function, though, we generally don't know what function we're
calling. Not only that, but we're probably not going to write
the called function ourselves -- or at any rate there's usually
some way for a third party to write a brand-new function and
get our code to "call back" to it. The set of possible functions
that might be called back is open-ended and usually extensible.

It's a mechanism for postponing the decision about what
function to call. Instead of making that decision when you
write the code, you can make it at run-time and you can choose
a function that didn't even exist when the call was written.
This ability to postpone the choice gives great flexibility,
and allows you to use a "plug-in" style to extend the capabilities
of a piece of code.

What are the required mechanisms? First, you need a way
to write a call to a function whose name isn't known -- in C,
the function pointer provides this capability; other languages
use other mechanisms. Second, you need some kind of protocol
that allows somebody to set your function pointer to point at
the function he'd like you to "call back" to. This can be as
simple as just providing the pointer as an argument, as in
qsort(), or maybe there's a way to "register" the function,
as with atexit().

Your timer example would probably use a registration-style
protocol. There'd be a function that adds a function pointer
to the list of functions to be called at each tick, and perhaps
another function to remove and "de-register" the callback. Each
interested module would register its own callback as part of its
initialization (probably), and the code that detects timer ticks
would simply run down the list of pointers, calling each callback
in turn. Fancier schemes are, of course, possible.

Is there a better way? Maybe, maybe not: "Better" is in the
eye of the beholder, and a mechanism than blends in well with
one program may prove awkward in another.

--
Er*********@sun.com

Nov 14 '05 #2
On Fri, 06 Aug 2004 18:23:59 GMT, Dave Boland
<NO************@stny.rr.com> wrote in comp.lang.c:
I read a lot of discussion about callback functions as used in C (as in
qsort for example). I'm comfortable with this as it is really a
function pointer. What I'm not totally clear about is how (or if) a
callback differs from an event such as GUI's use. One of the things I
found about events, at least in a VB6 project I did, is that an event
can be broadcast to all objects and they can use it if desired (code
only and GUI objects). Each object has to have a receive function for
that event though.

The only way I can see to do event broadcasting with a callback is to
have a special callback that in turn calls a list of receiver functions
that would be in each module. For example, a timer that broadcasts a
time tick every second would call a function (for example)
Timer_1min(void) that in turn would call functions in all of the modules
that need to know the time. I've also thought about a message que, but
that is begining to sound like too musch work. Is there a better way?


The better way, if you are serious, is to use message queues. No
other method scales as well. Similar approaches using things like bit
flags and such can work well for relatively small applications, but
hit a brick wall quickly as complexity increases.

And before anybody objects, it is quite possible to implement systems
based on message queues, or event flags, in standard C that have
nothing to do with multitasking, preemption, or threads.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: HankC | last post by:
Greetings: I'm been programming Delphi for a while and am trying to get into the Python mindset. In Delphi, you can have a component class that has properties, methods, and events. Properties...
0
by: ivan_r_moore | last post by:
Hello, Does anyone know how to do the Python equivalent of the Ruby WIN32OLE_EVENT example in: http://www.rubycentral.com/book/lib_windows.html in which WIN32OLE_EVENT is used in...
5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
10
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has...
8
by: Michael McDowell | last post by:
I'm confused: "why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler" and "how does making a...
1
by: Niklas | last post by:
Hi I have a remoting client/server application. The clients subscribes to a server event. All works fine if client is used as a window application, but when the client is used with No-Touch...
2
by: Edgardo Rossetto | last post by:
Hey everyone: Do callbacks need to be static? I'm trying to implement events for some async operations with I/O and network, but in all the examples I've seen the callbacks are static, making...
5
by: Adam Clauss | last post by:
Couple questions: 1) I have an application using TCP sockets. When I make a call to BeginReceive(), is the callback I specify called in the current thread or from a new thread? 2) Similar to...
5
by: WAkthar | last post by:
Hi, can anyone show me how to be able to pass the name of method to a dll function which will call this method when something inside the dll calling function happens. Is this called a callback?...
2
by: Hugh | last post by:
The PyGUI website specified this place as the place for general discussion about it, so here goes.... First off - thanks for something that is so straightforward to get running on OSX... I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.