473,387 Members | 1,353 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,387 software developers and data experts.

Why callback functions are static?

I need to use Asynchronous Socket functions in a server application and
am learning from sources such as the MSDN2
(http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx). What I
observed is that all callback handlers in examples are static
functions. I did try non-static callback handlers; they certainly
works, and they usually make my code simpler.

For example, a typical tutorial will start an asynchronous connection
with:

client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback),
client );

and handle the results with:

static void ConnectCallback(IAsyncResult ar) {
...
}

Because we cannot directly refer to any class member variables and
functions in a static function, we will have to pack them into the "ar"
parameter and pass them to the callback handler. In the above example,
we passed the caller of the BeginConnect() function as the "ar", so we
can get it back with "ar.AsyncState".

On the other hand, if non-static callback handlers are used, I usually
don't need to pass anything in "ar". In the above example, I will be
able to directly use the "client" in the ConnectCallback() (My
BeginXXX() and handlers are in the same class).

Can somebody please let me know if there will be any performance
penalties if I go with the non-static callbacks? What was the reason
for the penalties if there are any?

I am also curious about how expensive the "new" operation, that coverts
a callback function name to an AsyncCallback delegate, is. In case of
reading and writing, my sever will need to process lots of them. So I
am thinking of creating a member variable to hold the function
references for the lifetime off an object.

Any input will be very appreciated,
Jimmy

Oct 5 '06 #1
4 5847
No performance problem; in the examples, the Connect method itself is
static, so this is just a feature of the design of the example.
Delegates work similarly (identically?) for static and non-static
members, and instance will work fine.

This type of thing is often the result of simple demo code falling
directly from the (enforced) static Main() method...

Marc

Oct 6 '06 #2

Marc Gravell wrote:
No performance problem; in the examples, the Connect method itself is
static, so this is just a feature of the design of the example.
Delegates work similarly (identically?) for static and non-static
members, and instance will work fine.

This type of thing is often the result of simple demo code falling
directly from the (enforced) static Main() method...

Marc
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads. So it makes the example simpler and architectually
correct, but certainly more confusing. I could well be wrong though :)
Great question imo.

Oct 6 '06 #3
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads.
Curiously, I always approach it the exact opposite way - i.e. an
instance (non-static) method may, under cetain circumstances, have to
be made thread safe, but static methods should *always* consider thread
safety. Just being static doesn't (alone) enforce anything re thread
safety, and static methods can happily reference static members, and so
need sync. Conversely, with a non-static implementation, there is a
reasonable chance (depending on the design) that each instance
represents a separate request, and so we don't have to worry about sync
- as each callback will be acting on a different instance.

But yes - an intersting question / discussion topic.

Marc

Oct 6 '06 #4
Thanks both of you! I was busy writing a piece of code testing the
performance. It turns out, you guessed it, they are the same and Marc
was right.

Again, really appreciated!
Jimmy

Marc Gravell wrote:
I always assumed they did it that way so they didn't have to worry
about concurrent access to member variables where a callback is raised
from two threads.

Curiously, I always approach it the exact opposite way - i.e. an
instance (non-static) method may, under cetain circumstances, have to
be made thread safe, but static methods should *always* consider thread
safety. Just being static doesn't (alone) enforce anything re thread
safety, and static methods can happily reference static members, and so
need sync. Conversely, with a non-static implementation, there is a
reasonable chance (depending on the design) that each instance
represents a separate request, and so we don't have to worry about sync
- as each callback will be acting on a different instance.

But yes - an intersting question / discussion topic.

Marc
Oct 8 '06 #5

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

Similar topics

1
by: pvdm | last post by:
Hi, I am writing an app which encapsulates a multimedia timer. I implemented a TimerProc as static member function and a static member variable pThis as pseudo this variable to access in the...
11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
3
by: Wen | last post by:
hello, now, i wanna port a c++ program into C# project. a DLL written by C++ and keep it no change, and UI wirtten by C#. my C++ UI code is as below: // error handlers --global function...
4
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
4
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High...
2
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.